Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ASSIGNMENT #11 PART A: The factorial function n! is defined by n! n x (n-1) x (n

ID: 3835080 • Letter: A

Question

ASSIGNMENT #11 PART A: The factorial function n! is defined by n! n x (n-1) x (n 2) x x 3 x 2 x 1 and 0! is defined to be 1. n should always be greater than or equal to 0 For example, 3! 3x2x1 6. Write a C++ program that a user to enter an integer and display the factorial of that number on the screen, Program Requirements: You have to write a function to calculate a factorial of an integer The program should ask user to enter an integer The program should ask user whether to repeat or quit the program at the end of each execution. If the user does not enter a nonnegative integer number, the program should display a warning and ask again. (The number has to be integer too.) The program should display the output on the screen. You should run the program for the following 4 numbers in sequence and quit the program. Capture the output screen. o -3, 0, 5, and 10 Do NOT use recursion for this part

Explanation / Answer

#include <iostream>

using namespace std;

unsigned long long factorial(int n)
{
unsigned long long fact = 1;
if(n == 0)
return fact;
for(int i = 1; i < n; i++)
{
fact *= i;
}
return fact;
}

int main()
{
while(true)
{
double n;
while(true)
{
cout << "Enter a non negative integer to calculate its factorial: ";
cin >> n;
if (n < 0)
{
cout << "Enter a non negative integer" << endl;
continue;
}
else
{
if(n != ((int)n))
{
cout << "Enter an integer only" << endl;
}
else
{
break;
}
}
}
cout << factorial((int) n) << endl;
cout << "Do you want to continue (Y/N): ";
char choice;
cin >> choice;
if(choice == 'N' || choice == 'n')
{
break;
}
}

return 0;
}