Home >>Cpp Programs >Factorial program in C++

Factorial program in C++

Factorial program in C++

The product of an integer and all the integers that lies below it is known as the factorial. The Factorial program in C++ is basically a program that is used to display the factorial of an integer that is entered by the user as an input. Please note that the factorial of N is denoted by the N!.

Let's derive the factorial of 5 then it will be:

5! = 5*4*3*2*1 = 120

The most widely use of the factorial generally lies in combination and permutations in mathematics.

Different Ways through which the factorial program in C++ language can be written

There are numerous ways through which one can write the factorial program in C++ language. Here are the two most commonly used ways to write the factorial program as depicted below:

  • Factorial Program using loop
  • Factorial Program using recursion

1. Factorial Program using Loop

Here is the example

#include <iostream>  
using namespace std;  
int main()  
{  
   int i,f=1,num;    
  cout<<"Please Enter any Number to print factorial : ";    
 cin>>num;    
  for(i=1;i<=num;i++)
  {    
      f=f*i;    
  }    
  cout<<"Here is the Factorial of " <<num<<"  "<<f<<endl;  
  return 0;  
}
Output :
Please Enter any Number to print factorial : 6
Here is the Factorial of 6 720

2. Factorial Program using Recursion

Here is the example

#include<iostream>    
using namespace std;      
int main()    
{    
int fact(int);    
int f,num;    
cout<<"Enter Your number to print Factorial ";    
cin>>num;    
f=fact(num);    
cout<<"Here is the Factorial of given number : "<<f<<endl;    
return 0;    
}    
int fact(int n)    
{    
	if(n<0)    
	{			
	return(-1); /*if number is less than 0 W*/      
	}
	if(n==0)    
	{
	return(1);  /*if number is 0 then Terminate the  condition*/    
	}
	else    
	{    
	return(n*fact(n-1));        
	}    
}  
Output :
Enter Your number to print Factorial 6
Here is the Factorial of given number : 720

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++
No Sidebar ads