LogFAQs > #910494013

LurkerFAQs, Active DB, DB1, DB2, DB3, Database 4 ( 07.23.2018-12.31.2018 ), DB5, DB6, DB7, DB8, DB9, DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicCan anyone help me with this c++ program?
Yellow
10/14/18 1:27:15 AM
#5:


first off, you're using "while" incorrectly in main. You should be using "if" instead

if (n >= 0)
{
if (Prime(n) == 0)
cout << n << " is a prime number.";
else
cout << n << " is not a prime number.";
}
return 0;


'While' is meant to loop code and will repeat as long as the condition is met. 'if' will only go through once, which is what you're going for here I imagine.

If you want to loop through every number up until 12, you would have to use a 'for' loop in your main function.

int main()
{
int n;

cout << "Calculate every number up until: ";
cin >> n;

for (int i = 0; i < n; i++)
{
if (Prime(i) == 0)
cout << i << " is a prime number.\n";
else
cout << i << " is not a prime number.\n";
}
return 0;
}

Also, a little tip, to do line ends you can type \n in the middle of the string. That makes the output much easier to read. You can also use endl
http://www.cplusplus.com/forum/beginner/2138/
... Copied to Clipboard!
Topic List
Page List: 1