Using C++ program. Only C++. The factorial of a nonnegative integer n is written
ID: 3593971 • Letter: U
Question
Using C++ program. Only C++.
The factorial of a nonnegative integer n is written n! (pronounced “n factorial”) and is defined as follows n! = n * (n – 1) * (n – 2) * … * 1 (for value of n greater than to 1) and n! = 1 ( for n = 0 or n = 1). Write implementation a function factorial with the following function prototype long factorial (long n); Write the main function that uses the factorial function to compute and output the factorial of n values 0..10.
Example output:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Explanation / Answer
#include <iostream>
using namespace std;
long factorial (long n);
int main()
{
for(long i=1;i<=10;i++) {
cout<<i<<"! = "<<factorial(i)<<endl;
}
return 0;
}
long factorial (long n){
long product = 1;
for(long i=1;i<=n;i++) {
product *= i;
}
return product;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.