+ 3

How can I write c++ program to print factorial of 10

24th Mar 2018, 2:26 PM
MPUHWE Arsene
MPUHWE Arsene - avatar
5 Answers
+ 25
#include <iostream> using namespace std; int main(){ int a; cin>>a; int f=1,b=a; for(b;b>=2;b--) /*or U can run from lower to higher ... from 2 to b ... as U have initially taken f to be 1 & 0!=1!=1 ... so better to start from 2! only , why waste even 1 cycle */ f*=b; if(a>=0) //for whole numbers only cout <<f; return 0; } //or U can make 1-line recursion , just reduce the multiplication factor by 1 each time & call funcn again until that factor reaches to 1
24th Mar 2018, 6:19 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
use a loop. in each cycle decrement the variable starting with 10 by one and multiply it with the updated value of the variable.
24th Mar 2018, 2:47 PM
storm
storm - avatar
+ 3
well, I did something like an exercise. check out if it works. https://code.sololearn.com/cQu3e55oJJ5Q/?ref=app
24th Mar 2018, 6:11 PM
storm
storm - avatar
+ 2
thx let m try it
24th Mar 2018, 2:50 PM
MPUHWE Arsene
MPUHWE Arsene - avatar
+ 2
initialize f=1, i=1 do: f=f*i increment i while i<=10
24th Mar 2018, 3:21 PM
Akshay Karande