Task 1: Working with while loops (20 pts) The factorial of n, denoted n!, is def
ID: 3685902 • Letter: T
Question
Task 1: Working with while loops (20 pts)
The factorial of n, denoted n!, is defined as:
0! = 1
1! = 1
n! = 1*2*3*….*n for n > 1
So 4! = 1*2*3*4 = 24 and 6! = 1*2*3*4*5*6 = 720
Write an algorithm (pseudocode) to do the following:
prompt user to input an integer number n ( >= 0)
calculate the factorial of the number entered
output the result to standard output device
Create a flowchart for your algorithm using Raptor and verify it for the example cases listed above.
Modify your flowchart of Exercise 2 to valid the input using a loop that repeatedly prompts the user to enter a valid input (i.e. a number greater or equal to 0).
Write a C++ program for the flowchart of Exercise 3.
Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int i, n, factorial = 1;
cout<<"Enter a positive integer: ";
cin>>n;
for (i = 1; i <= n; ++i) {
factorial *= i; // factorial = factorial * i;
}
cout<< "Factorial of "<<n<<" = "<<factorial;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.