+ 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; }
4 Antworten
+ 17
umm also.. for loop doesn't need semicolon to end it after curly brackets..
{factorial*=i;}
+ 2
thaks
+ 1
you forgot to initialize variable factorial with 1(in ur code factorial has garbage value)
+ 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