Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

? Practice ? Write a function that uses for loop and the modulus (%) operator to

ID: 3568208 • Letter: #

Question

? Practice ? Write a function that uses for loop and the modulus (%) operator to detect whether an integer is a prime number. The function return true if this number is a prime number and false otherwise. Recall that a prime number is a number that is evenly divisible only by 1 and itself. Hint: the function prototype is bool primeCheck (int n) ; Write an main function that asks user to input a positive integer, in, and print to standard output the first m prime integers by calling function primeCheck.

Explanation / Answer


Indented code can be found at: http://ideone.com/AdMOpY
/******************************************/
#include <iostream>
using namespace std;

bool primeCheck(int n){
if(n <= 1) return false;
for(int i=2; i < n/2; i++){
if(n % i == 0) return false;
}
return true;
}

int main() {
int m;
cout << "Enter number m: ";
cin >> m;
cout << " First "<< m <<" prime numbers are: ";
int num = 2;
while(m != 0){
if(primeCheck(num) == true) {
cout << num << " ";
m--;
}
num++;
}
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote