Math in C++

Can someone please explain the math involved here with the function int factorial(int number). I can not seem to understand why int product =1 and how this particular for loop works to output the math of factorials and square roots. Thanks.

[code]
#include<iostream>
#include<cmath>
using namespace std;

int factorial(int number);

/* MAIN PROGRAM: */
int main()
{
int whole_number;

cout << "Enter a positive integer:\n";
cin >> whole_number;
cout << "The factorial of " << whole_number << " is ";
cout << factorial(whole_number);
cout << ", and the square root of " << whole_number << " is ";
cout << sqrt(whole_number) << ".\n";

return 0;
}
/* END OF MAIN PROGRAM */

/* FUNCTION TO CALCULATE FACTORIAL: */
int factorial(int number)
{
int product = 1;

for ( ; number > 0 ; number--)
product *= number;

return product;
}
/* END OF FUNCTION */
Factorial is the product of every integer up to the given integer.

For example.... 5! = 1*2*3*4*5 = 120

Your factorial function has a loop which counts down from the given number, and multiplies each number into the 'product' variable.

1
2
for ( ; number > 0 ; number--)
   product *= number;


So if number==5, the loop will run 5 times:

once where number==5
once where number==4
once where number==3
once where number==2
and
once where number==1

'product' get multiplied by number each time it loops. Therefore, once the loop completed, it will contain the factorial.
and the reason for no initiation of the for loop allows for any starting input? Also I get that now, thanks, that makes sense because once the loop reaches zero it will make the loop false and end it because 5x4x3x2x1x0 = 0.

However, what about

int product=1;

Why do you need to specify this?

and what about the square root function? Where is it being calculated in this code?
What Disch said. This type of function calling that occurs within the function itself is known as a recursive function.

edit: OOPS. I didn't read that closely enough, never mind.
Last edited on
This type of function calling that occurs within the function itself is known as a recursive function.


The factorial function is not calling itself, and therefore is not recursive.

JTG500 wrote:
and the reason for no initiation of the for loop allows for any starting input?


'number' doubles both as the number you're getting the factorial of and the loop counter.

'number' is already initialized because it was passed in as a parameter. There is not need to initialize it again.


int product=1;

Why do you need to specify this?


The product has to be initialized. If you do not initialize it, then it can start with any garbage number, which would throw off the results.

It's initialized with 1 because any number *1 is equal to itself.

and what about the square root function? Where is it being calculated in this code?


It's not. sqrt is part of the C/C++ standard library -- just like cout.

The code for it is hidden, just like the code for cout is hidden.
Last edited on
Thank you, I understand this a lot more now.
Topic archived. No new replies allowed.