Factorial of a Number in C++. Program to find Factorial of a Number in C++

Factorial : The product of all numbers from one to a number is the Factorial of that number. The factorial of 5 is 5 × 4 × 3 × 2 × 1 = 120. Factorial of 0 is 1. The Factorial is only defined for positive Integers.


 

Program to find Factorial of a Number

Logic :

  • Take the number
  • Pass the number to the function
  • Calculate the product of all numbers from one to given number
  • Print the Factorial

Code :

#include<bits/stdc++.h>
using namespace std;


int fact(int x)
{
    if(x==1)
    {
        return 1;
    }
    return x*fact(x-1);
    
}


int main()
{
    int n;
    cin>>n;
    cout<<fact(n);
    return 0;
}


***






!! SUBSCRIBE FOR MORE CODES & VIDEOS !!

Post a Comment

0 Comments