I have to write a code that will calculate the factorial of an entered number and when the number is negative the program will exit the loop and display the message "Goodbye". I am having trouble getting the program to display the message and then properly exit the loop.
#include <iostream>
usingnamespace std;
int main ()
{
unsignedint n = 1;
unsignedlonglong factorial = 1;
int i = 0;
while (n>=0)
{
cout << "Enter a positive integer (enter a negative number to quit) : ";
cin >> n;
if (n>0)
{
for (i=1; i <= n ; ++i)
{
factorial = factorial*i;
}
cout << "The factorial of " << n << " = " << factorial << endl;
factorial = 1;
}
else
{
cout << "The factorial of 0 is 1." << endl;
}
}
cout << "Goodbye!";
return 0;
} Put the code you need help with here.
currently n can't be neg, and he can't exit the loop because of it. If he makes the unsigned change, he will also need this logic ... good catch on that.