C++ Program to find factorial of a number using the function


In this program, You will learn how to find factorial of a number using the function in C++.


3! = 1 * 2 * 3 => 6


Example: How to find factorial of a number using the function in C++

Run it Now
#include<iostream>
using namespace std;

int factorial(int a) {

    int i, f = 1;
    for (i = 1; i <= a; i++) {
        f = f * i;
    }
    return f;
}

int main() {

    int a, f;

    cout << "Enter a Number :";
    cin>>a;

    f = factorial(a);
    cout << "\nThe Factorial is :" << f;

    return 0;
}

Output:

Enter a Number :4
The Factorial is :24