I know how to do it on paper, but not in code! HELP PLEASE!

Pages: 12
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.
Last edited on
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.
Here i made your first exercise im making the other ones too right now.Stay tuned!

P.S : If you don't understand something just post a comment.

Here is the code for the first exercise :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*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>
using namespace std;
bool isprime(int x)
{
    for(int i=2;i<=x/2;i++)
            if(x%i==0)
              return false;
    return true;
}
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;
}
Last edited on
Heres the second one (with the inner and outer loop):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
bool isprime(int x)
{
    for(int i=2;i<=x/2;i++)
            if(x%i==0)
              return false;
    return true;
}
int main()
{
    int n,i;
    cin>>n;
    for(i=2;i<=n;i++)
    if(isprime(i)) cout<<i<<' ';
    return 0;
}
Last edited on
Heres the third one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
    int n,t=2;
    cout<<"Please enter a positive integer greater than 1"<<endl;
    cin>>n;
    while(n>1)
    {
        if(n%t==0)
        {
            cout<<t<<' ';
            n/=t; //means n=n/t
        }
        else ++t; //means t=t+1
    }
    return 0;
}
Last edited on
closed account (iAk3T05o)
Get the jumping into c++ book. There's an example there and others.
It's a good book too.
Thanks guys, I'm trying really hard to learn this stuff.
Topic archived. No new replies allowed.
Pages: 12