C++ Example calculating the factorial of the entered number

C++ Example calculating the factorial of the entered number

June 28, 2024·İbrahim Korucuoğlu
İbrahim Korucuoğlu

In this example, we show how to calculate the factorial of the entered number with the help of a function.

#include <iostream>
using namespace std;

int factorial(int num) {
    int result = 1;
    for (int i = 2; i <= num; i++) {
        result *= i;
    }
    return result;
}

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;
    int factorialResult = factorial(number);
    cout << "Factorial: " << factorialResult << endl;

    return 0;
}
Last updated on