0

How to write c++ code to print the factorial of n?

16th Jul 2017, 7:32 PM
Waleed Yousif Taha Tai Allah
Waleed Yousif Taha Tai Allah - avatar
2 odpowiedzi
+ 9
int res = 1; int n; std::cin >> n; for (int i = n; i >= 1; i--) { res *= i; } std::cout << res;
16th Jul 2017, 7:46 PM
Hatsy Rei
Hatsy Rei - avatar
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; }
17th Jul 2017, 5:56 AM
Diana Pîşlac
Diana Pîşlac - avatar