factorial program using while loop

i want to better undestand whats going on with the code

Page 1 of 1

2 Replies - 22363 Views - Last Post: 16 September 2009 - 11:04 PM Rate Topic: -----

#1 yuggoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-September 09

factorial program using while loop

Posted 15 September 2009 - 03:46 PM

#include <iostream>
using namespace std;
int main ()
{
	int factor = 1;
	int n;
	cout << "enter a number to calculate its factorial value :\n";
	cin >> n;
	
	while (n >1) { 
	
		factor *= n--;
		
	}
	cout << "the answer is  " << factor << endl;
 
}



i dont know what the "*=" symbol means, so i dont know whats goin on with the code.

the program does work, but i want to better understand what each line means

can someone help me please

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: factorial program using while loop

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: factorial program using while loop

Posted 15 September 2009 - 04:17 PM

*= means take the left side of the equation, multiply it by the right side and set the left side to the result. So if factor is 5, and n is 4, after executing factor will be 20 (5 * 4). It's a shorthand way of saying factor = factor * n--;.
Was This Post Helpful? 1
  • +
  • -

#3 yuggoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 15-September 09

Re: factorial program using while loop

Posted 16 September 2009 - 11:04 PM

View PostJackOfAllTrades, on 15 Sep, 2009 - 03:17 PM, said:

*= means take the left side of the equation, multiply it by the right side and set the left side to the result. So if factor is 5, and n is 4, after executing factor will be 20 (5 * 4). It's a shorthand way of saying factor = factor * n--;.




great!!!!!

thanks a lot
Was This Post Helpful? 1

Page 1 of 1