Factorial program in c++

Factorial program in c++ is a simple program which is cpp program to find factorial of number in C++.

In this tutorial we will see program to find factorial in cpp,Factorial of given number is the mathematical problem.

Factorial of a number 

Factorial of a number N is calculated as N!.

Syntax to find factorial of a number

factorial

N! => N*(N-1)*(N-2)….

Example:  

Find factorial

calculate 6 factorial

6! =6*5*4*3*2*1 =720

factorial of a number 6 is 720.

Using this technique we implement the factorial in cpp.

We implement factorial program in cpp in three ways as :

  1. Factorial using for loop
  2. Factorial using recursion
  3. Factorial  using function.

Lets see Factorial in cpp

Factorial using for loop

This is first method using which we implement the factorial.

In this technique, we use the iterative loop as for loop to iterate and multiply the number up to N-1.

Write a program to find factorial of a number

123456789101112131415161718192021#include <iostream>using namespace std;int main(){int c, n, fact = 1; cout<<“Enter a number to calculate its factorial\n”; cin>>n;for (c=1;c<=n;c++) fact=fact*c; cout<<“Factorial of “<<n<<“= “<<fact;return 0;}

Output :

factorial program in cpp
factorial program in cpp

Explanation :

Factorial using for loop

1. Execution starts with the main method.

2. Declare variables

c,fact = > temporary variables

n => to store number

3. First we take input number which user want to find factorial

cin>>n;  =>  n=3

4. Next we calculate the factorial using for loop

for (c=1;c<=n;c++)

At Iteration 1 :

c=1, 1<=3 condition true loop executed

fact=fact*c; => fact= 1

At Iteration 2 :

c=2, 2<=3 condition true loop executed

fact=fact*c; => fact= 2

At Iteration 3 :

c=3, 3<=3 condition true loop executed

fact=fact*c; => fact= 6

At Iteration 4 :

c=4, 4<=3 condition false

5. Print the factorial of number.

=> cout<<“Factorial of “<<n<<“= “<<fact;

Factorial using recursion

Recursion : This is second method using which we implement program to find factorial.

In this technique, we recursively call the same function to find factorial.

What  is Recursion : Function calling itself is referred as recursion.

Factorial program in c++ using recursion

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#include <iostream>using namespace std;long factorial(int);int main(){ int n; long f;cout<<“Enter an integer to find its factorial\n”; cin>>n;if (n < 0) cout<<“Factorial of negative integers isn’t defined.\n”; else { f = factorial(n); cout<<n<<“! = “<<f; }return 0;}long factorial(int n){ if (n == 0) return 1; else return(n * factorial(n-1));}

Output :

factorial
factorial

Explanation :

Factorial using recursion

1. Execution starts with the main method.

2. Declare variables

n => to store number

f => to store factorial

3. First we take input number which user want to find factorial

cin>>n;  =>  n=3

4. Next we check number is valid 

if (n < 0) condition false

else executed

5. We call recursive function    

f = factorial(n) => factorial(3)

6 . Control transferred to function 

long factorial(int n) {

if (n == 0)    condition false

else

return(n * factorial(n-1));

=> 3*factorial(2); => function get call itself with n=2

we get value 6 => 3*factorial(2); => function get call itself with n=1

we get value 6 => 3*factorial(1); => function get call itself with n=0

if (n == 0)    condition true retur 1;

=> 1*2*3  each function call returns multiplication to main

}  => returns 6 to main

7. Print factorial of number returned by function

=>  cout<<n<<“! = “<<f;

Factorial using function 

This is third method using which we implement the factorial.

Function  : Function is a block of statement.

Factorial program in c++ using function

123456789101112131415161718192021222324252627282930313233343536373839#include <iostream>using namespace std;long factorial(int);int main(){ int number; long fact = 1;cout<<“Enter a number to calculate its factorial\n”; cin>>number; cout<<number<<“! = “<<factorial(number);return 0;}long factorial(int n){ int c; long result = 1; for (c = 1; c <= n; c++) result = result * c; return result;}

Output :

factorial
factorial

Explanation :

Factorial using function

1. Execution starts with the main method.

2. Declare variables

number => to store number

fact => to store factorial

3. First we take input number which user want to find factorial

cin>>number; =>  number=3

4. Next call the user defined function factorial(number)

long factorial(int n) => factorial(int 3)

c => teary local variable

result = 1 => store result local variable

for (c = 1; c <= n; c++)

At Iteration 1 :

c=1, 1<=3 condition true loop executed

result = result * c;  => result =1

At Iteration 2 :

c=2, 2<=3 condition true loop executed

result = result * c;  => result =2

At Iteration 3 :

c=3, 3<=3 condition true loop executed

result = result * c;  => result =6

At Iteration 4 :

c=4, 4<=3 condition false

return result; => returns 6 to main function

7. Print factorial of number returned by function