Compute Factorial of a Number

Compute Factorial of a Number” is very popular programming exercise problem for beginners. Here, we are given a number, enter by user and our task is to compute factorial of a number.

Factorial of a N:
N! = N*(N-1)*(N-2)*..*2*1.

Example (Compute Factorial of a Number):

INPUT:
N = 5
OUTPUT:
120

C++ Program to compute factorial of a number is as follows:

/* C++ Program to compute factorial of a number */
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int num;  
    //Scan the number  
    cout<<"Enter the number:";  
    cin>>num;  
      
    //Computing factorial  
    int factorial = 1;  
    for(int i = num ; i >= 1 ; i--)  
    {  
        factorial = factorial * i;     
    }  
      
    //Printing the factorial  
    cout<<"Factorial of "<<num<<" is "<<factorial;  
}  

OUTPUT:
Enter the number: 5
Factorial of 5 is 120

Related Posts:

  1. Program to find GCD of two numbers.
  2. Program to find LCM of two numbers.
  3. Program to check whether entered number is odd or even.
  4. Program to check whether entered number is prime number or not.
  5. Program to check whether entered number is palindrome or not.
  6. Program to check whether entered number is Armstrong Number or Not.
  7. Program to convert binary number to octal number.
  8. Program to convert binary number to decimal number.
  9. Program to convert binary number to hexadecimal number.
  10. Program to convert octal number to binary number.
  11. Program to convert octal number to decimal number.
  12. Program to convert octal number to hexadecimal number.
  13. Program to convert decimal number to binary number.
  14. Program to convert decimal number to octal number.
  15. Program to convert decimal number to hexadecimal number.
  16. Program to convert hexadecimal number to binary number.
  17. Program to convert hexadecimal number to octal number.
  18. Program to convert hexadecimal number to decimal number.
  19. Program to check Leap Year.
  20. Program to find sum of first ‘n’ natural numbers.

You may also like...