#include <iostream>
#include <cmath>
usingnamespace 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;
}
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.
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
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";
}
#include <iostream>
#include <cmath>
usingnamespace 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.