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++.

#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 << "Factorial is:" << f;

    return 0;
}

Output:

Enter a number:3
Factorial is:6