0
How to write c++ code to print the factorial of n?
2 Answers
+ 9
int res = 1;
int n;
std::cin >> n;
for (int i = n; i >= 1; i--)
{
res *= i;
}
std::cout << res;
0
This one will print you the first n factorials.
#include <iostream>
using namespace std;
int main()
{
int n, fact=1, i;
cout << "n:";
cin >> n;
for (i = 1; i <= n; i++)
{
fact = fact*i;
cout << i << "! = " << fact<<endl;
}
return 0;
}