Since it is a for loop, the counter is incremented till it is equal to
the number you input, and the expression total *= counter is executed
till the for loop is exited.

total  *= counter is nothing but total = total * counter; I hope you got
it.

 

Being new to programming and c++, I am unable to understand exactly 
what is going on in this example factorial program out of the c++ 
Demystified book. When I input a 3, it returns 6 and I'm not sure how. 
The thing that confuses me is the line that says total *=counter; If 
total = 1, and counter = 3(which is the number I inputted), then total 
*=counter should read as 1 = 1 * 3 which should output a 3. Can someone 
clarify this for me? 

#include <iostream>
using namespace std;
int main()

{ 
int number, counter, total = 1;
cout <<"Enter a number to see the factorial: ";
cin >> number;
cout << "The factorial of " << number << " is ";
for (int counter = 1; counter <= number; counter++)
total *= counter; //how can this return a 6 since total = 1 and counter 
= 3 ??
cout << total;
return 0;
}

 



[Non-text portions of this message have been removed]

Reply via email to