954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with Prime number code.

Hey guys, I know you wont give me a direct answer, but that's not what I'm looking for. Here is the assignment:

"Procedure:

1. Have the user input an integer between 2 and 19 (inclusive). If the number inputted is not within that range, reprompt the user again until a valid integer is entered.
2. Determine whether the number is odd or even, and display a message including the number that was inputted and whether that number is odd or even.
3. For all integers between 2 and the number inputted, determine whether each of those integers is a factor of the number inputted. If a number is a factor, display a message for each integer that is a factor.
4. While you are determining these factors, keep track of how many factors there are. If the numbers of factors is 0, the number is a prime number. Display a message that the number is prime. If it has factors (i.e. the number of factors is > 0 ), display a message that the number is not prime.
"

here is what I have so far:

//Andrew Kraska
// Prime or Not?
#include <iostream.h>

int main()
{
int x;

cout<< "Input a number between 2 and 19"<<endl;
cin >> x;

if (x<2)
{
cout<< "Please enter a number BETWEEN 2 and 19"<<endl;
}


if (x>19)
{
cout<< "Please enter a number BETWEEN 2 and 19"<<endl;
}


if (x%2 != 0)
{
cout << "This number is odd" << endl;
}

if (x%2 == 0)
{
cout<< "This number is even" << endl;
}

for(int i = 1; i < 20; i++)
{
if( x % i != 0)
{
cout <<"This number is prime"<< endl;
break;
}
}

return 0;
}

blinkliveson
Newbie Poster
3 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

And what is the question?

Um, ok... I'll give you a hint... something about your for loop is wrong. It will always return that number is prime. Try to backtrack you program if you enter for example number 4. It shouldn't be prime, but... :)
And for all those if-statements... try using some else if too

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You