A prime number (or a prime) is a natural number greater than 1 which has exactly
ID: 3619904 • Letter: A
Question
A prime number (or a prime) is a natural number greater than 1 whichhas exactly two distinct natural number divisors: 1 and itself.
An infinitude of prime numbers exists, as demonstrated by Euclid
around 300 BC. The first thirty-four prime numbers are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139.
The integer 4 is not a prime number since it has three divisors: 1, 2, and 4.
Given an integer value, please write a program to check if the value is a prime number.
*/
#include <iostream>
using namespace std;
int main( )
{
int value; //contains the given integer
int current; //the current integer to be evaluated if it is a divisor of value
bool isPrime; //If true, value is a prime number;
//otherwise, value is not a prime number.
cin >> value; // read the given integer
Complete the program so that isPrime contains true if value is a prime number. A prime number is a natural number greater than 1 which has exactly two distinct divisors: 1 and itself.
This is what I have don so far
for(current= 2; current<= value; current++)
{
int i= 2;
isPrime= true;
while(i<= sqrt(current))
{
if(current%i==0)
isPrime= false;
i++;
}
}
if ( isPrime )
cout << "The integer " << value << " is a prime number!" << endl;
else
cout << "The integer " << value << " is not a prime number since the integer "
<< current << " is a divisor of " << value << "!" << endl;
return 0;
}
Explanation / Answer
please rate - thanks here is your corrected code. I changed the output for the non prime numbers, since you may not learned enough to do it properly. for example the number 6 has factors 2 and 3, whereas your remark would imply that it has only 1 factorRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.