#include <iostream>
using namespace std;
int main(){
int input;
cin >> input;
while (input >= 1);{
if (0 == input % 2);
cout << input << " is not a prime number." << endl;
if (0 != input % 2);
cout << "something else" << endl;
cin >> input;
}
cout << input;
system ("pause");
return 0;
prime number
Page 1 of 113 Replies - 1134 Views - Last Post: 14 February 2010 - 11:05 PM
#1
prime number
Posted 13 February 2010 - 07:44 PM
I have to determine if a number is prime or not. I am just starting out with this code and have already ran into a problem: I will input a number, and then nothing happens.
Replies To: prime number
#2
Re: prime number
Posted 13 February 2010 - 07:49 PM
If you're using a while statement, you might aswell do a "do-while" statement.
#include <iostream>
using namespace std;
int main(){
int input;
cin >> input;
do {
if (0 == input % 2);
cout << input << " is not a prime number." << endl;
if (0 != input % 2);
cout << input << " is not prime." << endl;
cin >> input;
} while (input >= 1);
// cout << input; <----- Why have the input printed twice? It's printed above.
// and also, you should probably just ask the user if they
// wanna enter another number every time.
system ("pause");
return 0;
This post has been edited by IngeniousHax: 13 February 2010 - 07:50 PM
#3
Re: prime number
Posted 13 February 2010 - 08:04 PM
while (input >= 1);{
The semi-colon following the while statement -- if input is indeed >= 1 -- results in an infinite loop that does nothing except eat CPU.
#4
Re: prime number
Posted 13 February 2010 - 08:09 PM
Your program gets stuck in the infinite while loop. You need to remove the semicolons after the "while" and "if" statements, which in fact mean the end of the statement body.
...
while (input >= 1){
if (0 == input % 2)
cout << input << " is not a prime number." << endl;
if (0 != input % 2)
cout << "something else" << endl;
cin >> input;
}
#6
Re: prime number
Posted 13 February 2010 - 08:55 PM
I never knew about the syntax while(statement);. Thanks!
#7
Re: prime number
Posted 14 February 2010 - 05:42 PM
Can someone check this for me to see if it is correct. The user inputs a number and I output if it is prime or not. thanks.
#include <iostream>
using namespace std;
int main(){
int input;
cin >> input;
do
{
if (input <= 0 || input == 1 || (input % 2 == 0 && input != 2) || 0 == input % 3 || 0 == input % 5 || 0 == input % 7 || 0 == input % 9)
cout << "number is not prime" << endl;
else
cout << "prime number" << endl;
cin >> input;
}
while (input > 0);
system ("pause");
return 0;
}
#8
Re: prime number
Posted 14 February 2010 - 09:57 PM
Why don't you compile it and find out?
#9
Re: prime number
Posted 14 February 2010 - 10:26 PM
well, the algorithm you're looking for...
--Edit--
I changed my mind, you're probably doing your homework. I'm not going to do it for you. Although I do have the working program and I accept paypal.
--Edit--
I changed my mind, you're probably doing your homework. I'm not going to do it for you. Although I do have the working program and I accept paypal.
This post has been edited by taylorc8: 14 February 2010 - 10:35 PM
#12
Re: prime number
Posted 14 February 2010 - 10:48 PM
It doesn't work. You can use a for loop from 2 to n, breaking if you find that it evenly divides.
#13
Re: prime number
Posted 14 February 2010 - 11:02 PM
My code is working. I am guessing you mean his? If you would like proof, I can supply it for $1.00
Page 1 of 1

New Topic/Question
Reply



MultiQuote





|