this is a factorial code by for loop...what is the fault in it?

#include
using namespace std;
int main()
{
int a , factorial;
int b;
cin>>a;
for(b=1;b<a;–b)
{factorial=b*1;
cout<<factorial;
}
return 0;
}

You seem t be very beginner, so let me explain you your code in a very basic manner.

Firstly, always put your code in proper blocks or give ideone link whenever you put on forum.
Secondly,

You should initialise very variable whenever you declare it. Here you should have done
int factorial=1;

Otherwise it’ll start from some garbage value and will give false results. Now, move to your for loop:
We need t compute lets say 4!, then

 we do this factorial=1; b=1;
    b=2, means 2*factorial(1) = 2;
    b=3 means 3* factorial(2)= 3*2=6;
    b=4 means, 4* factorial(3)= 4*6= 24;
    Thus, we can see that we are using the value of factorial computed in the previous iteration and multiply it with the value of b of the current iteration. Is this clear? Ask in comments if not!!

Also, we are at every stage incrementing the value of b, but you are decrementing it. Yourself, see, when b=1, it’ll be decremented to 0, and then it’ll always be less than a and hence will run into infinite loop. So, for loop should have been:

for(b=1;b<=a;b++)  // increment b
{
  factorial= factorial*b;
}
 //LHS correpsonds to the new value which will be assigned after the RHS executes, (assign  operator has left-to-right associativity), the factorial in the RHS corresponds to the value of factorial computed in the previous iteration and hence, we multiply it with our current value of b

PS: This seems that you don’t have good basic knowledge of operators and loop. Please go through them and surf web a little before asking!!

Happy to help :slight_smile: