-
post-decremented when?
Code:
#include <iostream>
using namespace std;
int main()
{
unsigned long factorial = 1,
number = 0;
cout <<" Enter number " ; // entered 12 as number
cin >> number;
for ( int counter = number; counter >= 1; counter -- ) // post increment counter
factorial *= counter; // value of counter is 12 * 12 ?
cout << " " << factorial; // answer
return 0;
}
Hi i was wondering in the for structure where i have counter--.
And i have then counter * counter ,
does it multiply 12 * 12 ? or 12 * 11 and when does it get post decremented?
Thanks alot.
-
Add some cout lines and see for yourself ;)
Code:
for ( int counter = number; counter >= 1; counter -- )
{
cout << "counter : " << counter endl
<< "factorial before: " << factorial <<endl;
factorial *= counter;
cout << "factorial after: " << factorial <<endl;
}
Also, this: -- is decrement. This: ++ is increment.
-
Hi hammer thanks alot i wrote wrong word.
I will try what you said
-
Ok here is output.
Enter number 12
1st 12
2nd 12
3rd 12
1st 11
2nd 132
3rd 11
1st 10
2nd 1320
3rd 10
1st 9
2nd 11880
3rd 9
1st 8
2nd 95040
3rd 8
1st 7
2nd 665280
3rd 7
1st 6
2nd 3991680
3rd 6
1st 5
2nd 19958400
3rd 5
1st 4
2nd 79833600
3rd 4
1st 3
2nd 239500800
3rd 3
1st 2
2nd 479001600
3rd 2
1st 1
2nd 479001600
3rd 1
Program exited normally.
(gdb)
Looks good .
Thanks alot for idea.