Factorial in C++


Q.Write a C++ program for Factorial Number.

#include <iostream>       
using namespace std;
 
int fact(int a){  
    if (a==0 || a==1){  
        return 1;  
    }  
    else{  
        return a*fact(a-1);  
    }  
} 
int main()
{ 
    int n;
    unsigned long factorial = 1;
    cout << "Enter the factorial number : ";
    cin >> n;
	factorial= fact(n);
	cout << "Factorial of " << n << " " << factorial;
	return 0;    
} 

Q. Write an algorithm to Factorial Number.

1. Start
2. Read integer `n`
3. Define function `fact(a)`:
   - If `a` is 0 or 1, return 1
   - Else, return `a * fact(a - 1)`
4. Call `fact(n)` and store result in `factorial`
5. Print "Factorial of n is factorial"
6. End 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.