+ 2

Hi! Can anyone check this code and tell me why it doesn't work ?

#include <iostream> using namespace std; /* this program is to find factorial of any number*/ int main() { int factorial,n,i; cout<< "enter any positive integer number:"<<endl; cin>>n; for (i=1;i<=n;i++) {factorial*=i}; cout << "factorial of "<<n<<"="<<factorial<<endl; return 0; }

7th Jul 2017, 3:10 PM
mahmood
mahmood - avatar
4 Answers
+ 17
umm also.. for loop doesn't need semicolon to end it after curly brackets.. {factorial*=i;}
7th Jul 2017, 3:18 PM
Frost
Frost - avatar
+ 2
thaks
7th Jul 2017, 3:50 PM
mahmood
mahmood - avatar
+ 1
you forgot to initialize variable factorial with 1(in ur code factorial has garbage value)
7th Jul 2017, 3:17 PM
Sandesh
Sandesh - avatar
+ 1
it should be {factorial *=I;} //semi colon should be inside the bracket and you must declare value of factorial as 1 otherwise the some value of factorial will be used for multiplication
7th Jul 2017, 3:28 PM
‎ ‏‏‎Anonymous Guy