I give up. I'm just going to take a zero for the assignment because there is no way I will be able to finish it. I have a headache just from one problem and I didn't even have a chance to do the others.
Is there any articles that help you solve problems like this, becuase I have no examples to go off of.
Just compare your code to the others. Basically how would you do this on paper?
Say we have a value of 13.
Is it prime?
divided by 2? No
divided by 3? No
divided by 4? No
divided by 5? No
divided by 6? No
divided by 7? no
divided by 8? no
divided by 9? no
divided by 10? no
divided by 11? no
divided by 12? No
It is a prime no numbers can be divided into it besides itself and 1.
So what did we do?
We looped from 2 -> n - 1
Then if it was divisible by a number evenly ( no remainder ) it is not a prime
If it finishes the loop and is still a prime. It is a prime.
1 2 3 4 5
for( int i = 0; i < n; ++i ) //or i <= n - 1
{
if( n % i == 0 ) //divides into it
prime = false; //not a prime
}
On paper I would just see if it was an even number, if not then you start dividing a set number of integers from like 2-5. I have the loop in my code, but no matter what number I put in it says its a prime number. I don't know what I'm supposed to read to fix this or answer the other problems I have, I still want to be able to finish it, but I don't know what to do. that's why I came here. My professor doesn't explain any of this, the book had 1 (poorly) explained page on booleans, by looking at your guy's code makes sense, but learning strictly from a book where there are poor examples is just hard.
/*case 25 - not prime
*case 120 - not prime
*case 97 - is prime
*case 21 - not prime
*case 9973 - is prime
*case 9977 - is not prime
*case 13 - is prime
*case 30 -is not prime
*/
#include <iostream>
usingnamespace std;
bool isprime(int x)
{
for(int i=2;i<=x/2;i++)
if(x%i==0)
returnfalse;
returntrue;
}
int main()
{
int n;
beginning:
cout<<"Please enter an integer greater than 1"<<endl;
cin>>n;
if(n<=1) goto beginning; //if the number is not greater than one it jumps to beginning
if(isprime(n)) cout<<n<<" is prime"<<endl;
else cout<<n<<" is not prime"<<endl;
return 0;
}