English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The following is an example of finding the factorial.
#include <iostream> using namespace std; int fact(unsigned long long int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int main() { unsigned long long int n; cout << "Enter number: "; cin >> n; cout << "\nThe factorial: " << fact(n); return 0; }
Output result
Enter number : 19 The factorial : 109641728
In the above program, we have declared a variable with the following data type.
unsigned long long int n;
Actual code offact()
The method is as follows-
int fact(unsigned long long int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); }
In themain()
In the method, the user enters a numberfact()
It is called. The factorial of the entered number is printed.
cout << "Enter number: "; cin >> n; cout << fact(n);