PrimeNumberTest_ ?.?

*make a prime number test using the for loop

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
30
31
32
33
34
35
36
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    int j;
    int is_prime = 1;
    
    while ( j != 0 )
    {
    cout << "Key in a number ( zero to terminate program ) and press ENTER: ";      
    cin >> j;
    j = sqrt(j);
    
    int a;
    
    for (a = 2; a <= j; a++)
    {
        if (j % a == 0)
        {
              is_prime = 0;
              break;
        }
    }
    
    if (is_prime)
       cout << "\nNumber IS prime\n\n";
    else
        cout << "\nNumber is NOT prime\n\n";
    }
    system ("PAUSE");
    return 0;
}


i cant figure out what is wrong with the code.
First thing I noticed is that you're using an int the way that you would use a bool.

Another thing is that you shouldn't system ("PAUSE");

Try cin.ignore(); instead.
system("PAUSE"); is ok. I see you are using dev c++. WIth just return 0; it will not pause for us to see. However, system("ANYTHING") is bad but only when you get to advanced programming. Prime Number program not important.

Last edited on
brother i feel u are somewhat wrong at the logic:
1
2
 j = sqrt(j);
    

has no use at all...just chuck it off!!!!!!ull get the results!!!
also u should use if (a % j == 0) instead of if (j % a == 0)

last but not the least, we learn sumthing about variable scope........u do not define is_prime=1 inside the while loop beacuse of which itll run correctly only once., after that itll forever have the value 0 once any non-prime number is encountered!!!!..what u shud do is each time define is_prime=1 inside the while loop/..........hope this helps!!!!!!!!!!!1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int is_prime = 1;
    
    while ( j != 0 )
    {is_prime=1;//this shud be done.......
    cout << "Key in a number ( zero to terminate program ) and press ENTER: ";      
    cin >> j;
    j = sqrt(j);
    
    int a;
    
    for (a = 2; a <= j; a++)
    {
        if (j % a == 0)
        {
              is_prime = 0;
              break;
        }
    }
    
    if (is_prime)
       cout << "\nNumber IS prime\n\n";
    else
        cout << "\nNumber is NOT prime\n\n";
    }
Last edited on
1) system("PAUSE") is NOT okay. http://cplusplus.com/forum/beginner/1988/#msg7275

2) Posting a preformed answer to a homework problem, and a badly formatted one at that, is also NOT okay. We're trying to get him to think, not steal.
kk, sorry about that! :p
But telling not necessary stuff to beginners is also not ok.
I know how much confused I was when you said that!
Last edited on
@OP: ¿what is wrong with the program?

biplav17 wrote:
I see you are using dev c++
o_O
yea in dev C++ when you start a new console : this is what it gives:
1
2
3
4
5
6
7
8
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
    system ("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
i changed it to this
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
30
31
32
33
34
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    int j;
    
    while ( j != 0 )
    {
    bool is_prime = true;
    cout << "Key in a number ( zero to terminate program ) and press ENTER: ";      
    cin >> j;
    
    int a;
    
    for (a = 2; a <= sqrt(j); a++)
    {
        if (j % a == 0)
        {
              is_prime = 0;
              break;
        }
    }
    
    if (is_prime)
       cout << "\nNumber IS prime\n\n";
    else
        cout << "\nNumber is NOT prime\n\n";
    }
    system ("PAUSE");
    return 0;
}

and now it works (:
thx for the help

@ciphermagi i wish they would teach programming in school, i got this from C++ Without Fear example 3.2. and i guess ill google how to properly format code.
pero muchas gracx q=
Your formatting isn't actually bad. It's concise without being cramped, and it's not holding on to white space just for the sake of it. When you get to college, if you're really that interested in it, you should be able to get a lot of hands-on help from people.
Topic archived. No new replies allowed.