This post contains a total of 25+ Hand-Picked C++ Factorial Calculator Examples with Source Code. All the Factorial Calculators are made using C++ Programming language.
You can use the source code of these programs for educational purpose with credits to the original owner.
Related Posts
Click a Code to Copy it.
1. By Onel Vega
Made by Onel Vega. C++ program to find the factorial of a number. ( Source )
#include <iostream>
using namespace std;
int factorial(int n){
if (n == 0){
return 1;
}
else {
int answer = n * factorial(n-1);
return answer;
}
}
int main() {
int a;
cin >> a;
cout << "Factorial " << a << " is equal to: " << factorial(a) << endl;
return 0;
}
2. By Jainil Raval 🇮🇳
Made by Jainil Raval 🇮🇳. A small program to find the factorial of a number. ( Source )
#include <iostream>
using namespace std;
int main()
{
float n,i,f=1;
cin>>n;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"\nFactorial is "<<f;
return 0;
}
3. By Aman kumar Singh
Made by Aman kumar Singh. ( Source )
#include <iostream>
using namespace std;
int main() {
int i,num,factorial;
factorial=1 ;
cout <<"enter the number\n";
cin >>num;
for(i=1;i<=num;i++){
factorial = factorial*i;
}
cout<<"factorial is - " <<factorial;
return 0;
}
4. By Mind To Machine 💻🕆
Made by Mind To Machine 💻🕆. Enter a number to get factorial. Given any positive integer, the factorial is the product of all the positive integers smaller or equal to that interger. e.g 5!=54321=120;, means factorial,so 5! is 120. ( Source )
#include <iostream>
int factorial(int);
int main() {
int a;
std::cin >> a;
std::cout << a <<"! is "<<factorial(a);
return 0;
}
int factorial(int j){
if(j==1){
return 1;
}else{
return j * factorial(j-1);
}
}
5. By Mukul
Made by Mukul. ( Source )
#include <iostream>
using namespace std;
unsigned long long factorial(int n) {
if (n==1) {
return 1;
}
else if (n==0) {
return 1;
}
else {
return n * factorial(n-1);
}
}
int main() {
int a;
cin >> a;
/*if (a<=20 && a>=0) {
cout << factorial(a);
}
else if (a>20) {
cout << "Infinity";
}
else if (!cin || a<0 || a != (int)(a)) {
cout << "Error";
}*/
if (a>20) {
cout << "Infinity";
}
else {
if (!cin || a<0 || a != (int)(a)) {
cout << "ERROR";
}
else {
cout << factorial(a);
}
}
}
6. By UrBoyO
Made by UrBoyO. Input a number to find its factorial. Numbers become large quickly! 32 and above either give negative answers or 0 (because they have reached the signed integer limit). ( Source )
#include <iostream>
using namespace std;
int factorial(int num)
{
if(num==0)
{
return 1;
}
else
{
return num * factorial(num-1);
}
}
int main()
{
int num;
cin >> num;
cout << factorial(num);
}
7. By Faisal Ahmed Moshiur
Made by Faisal Ahmed Moshiur. First enter 1 to select factorial after that enter the input number you want the factorial of. ( Source )
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
double fact (int N)
{
if (N==0||N==1) return 1;
if (N>1) return (N*fact(N-1));
}
double M_PIf(int N)
{
double i=0, count=0, x=0, y=0, seed;
seed=time(NULL);
srand(seed);
for(i=1; i<=N; i++)
{
x=(double)rand()/(double)RAND_MAX;
y=(double)rand()/(double)RAND_MAX;
if(sqrt(x*x+y*y)<=1) count++;
}
//I've included Pi function "M_PIf()" as this emulator can not show values of M_PI.
return 4*(double)count/(double)N;
}
double sine (double x,int N)
{
double sum=0;
x*=(M_PIf(10000)/180);
for(int i=1; i<=N; i++)
{
sum+=pow(-1,(i+1))*pow(x,2*i-1)/fact(2*i-1);
}
return sum;
}
double cosine (double x, int N)
{
double sum=0;
x*=(M_PIf(10000)/180);
for(int i=0; i<=N; i++)
{
sum+=pow(-1,i)*pow(x,2*i)/fact(2*i);
}
return sum;
}
double tangent (double x, int N)
{
return (sine(x,N)/cosine(x,N));
}
int main ()
{
double x;
int n, N, decision;
cout<<"To calculate factorial input 1\n To calculate sine input 2\n To calculate cosine input 3\n To calculate tangent input 4 \n";
cin>>decision;
if (decision==1)
{
cout<<"Please enter the factorial you want to calculate(N!): \n";
cin>>N;
cout<<fact(N);
}
else if (decision==2 || decision==3 || decision==4)
{
cout<<"Please enter the value of x (in degree) and n (no. of iterations): \n";
cin>>x>>n;
{
if (decision==2)
cout<<sine(x,n)<<endl;
}
{
if (decision==3)
cout<<cosine(x,n)<<endl;
}
{
if (decision==4)
cout<<tangent(x,n)<<endl;
}
}
return 0;
}
8. By Chimkaeme Ndu
Made by Chimkaeme Ndu. ( Source )
#include <iostream>
using namespace std;
int factorial(int x)
{
if(x==1) {
return 1;}
else {return x * factorial (x-1);}
}
int main()
{ int a;
cin>>a;
cout << factorial(a);
return 0;
}
// type in the no. dat u wanna find it's factorial
9. By Jade
Made by jade. Enter an integer, program will calculate the factorial and print out each whole number calculated in between. ( Source )
/* Factorial Printer
for loop, void function
Chapter 5, 5.1
C++ Without Fear, 3rd Edition (Overland)
Created: 10/21/18
First Successful Run: 10/21/18
*/
#include <iostream>
using namespace std;
void print(int x); //declare function before main
int main() {
int n;
int x = 1;
cout << "Hello! This is a factorial calculator. " << endl;
cout << "A factorial is the product of all whole numbers from 1 to N." << endl;
cout << "Please enter N: ";
cin >> n;
while (n <= 0) { //invalid input
cout << "Please enter a positive integer: ";
cin >> n;
}
for (int i = 1; i <= n; ++i) {
x *= i; // x = x*i where i increases until n
// x = 1*1; x= 1*2; x = 2 *3; etc
print(x); //call function print
}
cout << endl << n << "! = " << x;
return 0;
}
//define print function outside of main
void print(int x) {
cout << x << " ";
}
10. By Alexey Vishnetsky
Made by Alexey Vishnetsky. This calculator can count factorials up to number 20. it is also protected from inputting wrong input. ( Source )
#include <iostream>
using namespace std;
unsigned long long int factorial(int n) {
if (n==1) {
return 1;
}
else {
return n * factorial(n-1);
/* multiplies a number by number-1 till its multiplied by 1*/ }
}
int e;
int main() {
cin >> e;
if (e>20) {
cout<<"The number is too big"; /*numbers above 20 are counted wrong */
}
else {
if (!cin || e <= 0 || e != (int)(e)) {
cout << "Please enter a natural number";
/*Protects you from typing anything but natural numbers */
}
else {
cout << factorial(e);
/*outputs the factorial*/
}
}
}
11. By Gustav Rödel
Made by Gustav Rödel. ( Source )
#include <iostream>
using namespace std;
void factorial(){
int x, i;
int result = 1;
cin >> x;
for(i = 0; i < x; i++){
result *= (i+1);
}
cout << "Factorial of " << x << " is: " << result << endl;
}
int main() {
factorial();
return 0;
}
12. By xX randomryze Xx
Made by xX randomryze Xx. ( Source )
#include<iostream>
using namespace std;
int main()
{
long long num = 1;
long long fac = 1;
cout << " Enter Number To Find Its Factorial: ";
//Enter number that u'll transform into factorial
cin >> num;
cout << endl;
for (int x = 1; x <= num; x++)
{
fac = fac*x;
}
//The output and the factorial number
cout << " Factorial of Given Number"<< num <<" is = " << fac << endl;
return 0;
}
13. By Yuvraj Dhruw
Made by Yuvraj Dhruw. Factorial calculator (use of Array and Dynamic Memory Allocation) ( Source )
#include <iostream>
using namespace std;
int main()
{
cout<<" Factorial calculator"<<endl<<endl;
int x, sum=1;
cout<<"Enter number whose factorial you wanna find: ";
cin>>x;
int *arr= new int [x]; //Dynamically allocating memory for an array.
cout<<endl<<x<<"! = ";
while(x!=0)
{
arr[x]=x;
sum*=arr[x];
x--;
}
delete [] arr; //Never forget to free the memory after use.
cout<<sum<<endl;
//this is stupid use of array; if using just to calculate the factorial, but i didn't got anyhing else in my mind for the use of an array.
system ("pause");
return 0;
}
//EDIT: I also added a bit of dynamic memory allocation part.
14. By Saleh M Ashumrani
Made by Saleh M Ashumrani. ( Source )
/*****************************************
* Date Created: 04/22/2018
*****************************************/
#include <iostream>
#include <iomanip>
using namespace std;
/* When prompted Enter a number to get it's Factorial */
unsigned long long int Fact(unsigned long long int num);
int main()
{
int num = 0;
cout << "Enter a number to calculate it's Factorial:" << endl;
cin >> num;
cout << "\nThe Factorial for " << num << "! " << "is " << Fact(num) << endl;
return 0;
}
/************************************************
*Purpose: This function Calculates the Factorial
* for the inputed number
************************************************/
unsigned long long int Fact(unsigned long long int num)
{
if (num == 0)
{
return 1;
}
else
{
return (num * Fact(num - 1));
}
};
15. By Iyad Ahmed
Made by Iyad Ahmed. Simple C++ Factorial Calculator with safe input + overflow check. ( Source )
#include <iostream>
#include <cmath>
#include <climits>
using namespace std;
double safein(string ask){
cout << ask << endl;
double x;
while(!(cin >> x) || x - floor(x)){
cout << "Not an integer!\n";
cin.clear();
cin.ignore(256,'\n');
}
return x;
}
void factorial(int x){
bool overflow = false;
unsigned long long out = 1;
for(int i = 2; i <= x; i++){
if (out > ULLONG_MAX/i){
overflow = true;
break;
}
out *= i;
}
if(overflow){
cerr << "Overflow: Result is bigger than max. unsigned long long" << endl;
}
else{
printf("Factorial(%d) = %llu\n", x, out);
}
}
int main() {
int x = safein("Enter an integer");
cout << x << endl;
factorial(x);
return 0;
}
16. By Andenis
Made by Andenis. Enter a number and this code will calculate its factorial. ( Source )
#include <iostream>
using namespace std;
int main()
{
long long factorial = 1;
int a;
cin >> a;
for(int i = 1; i <= a; i++)
{
factorial *= i;
}
if (factorial != 0)
{
cout << "Factorial of number " << a << "! = "<< factorial;
}
else
{
cout << "This number is too high, try using something between 0 and 65";
}
}
17. By Vcelist
Made by Vcelist. Enter the method you want to use, f for factorial calculation, rf is for reverse factorial calculation. ( Source )
#include <iostream>
using namespace std;
int main() {
string method;
cin>>method ;
if (method=="F" || method=="f") {
int a,n;
cin >> n;
cout << "The factorial of "<< n;
a=n-1;
while(a!=1) {
n=n*a;
a--;
}
cout << " is " << n;
}
if (method=="RF" || method=="rf") {
int num, n, a;
cin>>num;
cout << num << " is the factorial of ";
n=1;
a=0;
while(n!=num)
{a++;
n=n*a;
}
cout<<a;
}
return 0;
}
18. By Trent
Made by Trent. ( Source )
#include <iostream>
#include <cmath>
using namespace std;
// Calculates the answer of the valid inputs for a factorial.
unsigned long long factorial(unsigned long long num)
{
unsigned long long total = 1;
for (unsigned long long i = 1; i <= num; ++i)
{
total = i * total;
}
return total;
}
// Determines whether the user input is a valid option
// and if not gives the user an error or the result.
unsigned long long range(unsigned long long userNum)
{
if (userNum > 0 && userNum < 64)
{
cout << "The factorial of " << userNum << " is " <<
factorial(userNum) << ". \n\n";
}
else if (userNum <= 0)
{
cout << "Factorial must be positive numbers. \n\n";
}
else {
cout << "Overflow error. Please use a smaller number. \n\n";
}
return 0;
}
// prompts the user and asks for input.
int main()
{
cout << "Type in an integer to be factorialized. \n";
unsigned long long a;
cin >> a;
range(a);
return 0;
}
19. By Ananda Fitra
Made by Ananda Fitra. ( Source )
#include <bits/stdc++.h>
using namespace std;
int factorial(int n) {
if (n==1) {
return 1;
}
else {
return n * factorial(n-1);
}
}
main() {
long long int a, hasil;
cout << "Factorial Calculator" << endl << endl;
cout << "Type an integer for factorial calculation : " ; cin >> a;
hasil = factorial(a);
cout << endl << "The factorial answer of " << a << " is " << hasil;
}
20. By ashay
Made by ashay. ( Source )
#include <iostream>
using namespace std;
int main()
{
int n,fact; fact=1; cin>>n; while(n>=1){fact=fact*n; n=n-1;} cout<<fact;
return 0;
}
21. By Fábio Melo
Made by Fábio Melo. ( Source )
#include <iostream>
using namespace std;
//declares & defines the recursive function (calculatey bit)
int factorial(int n){
if (n==1){
return 1;
}
else{
return n*factorial(n-1);
}
}
//main fuction (the cally/printy bit)
int main() {
int num;
cout<<"Factorial calculator 1.0"<<endl<<"Enter a number"<<endl;
cin>>num;
cout<<num<<"! = "<<factorial (num);
}
22. By Sanchit Raina
Made by Sanchit Raina. Basic C++ program to find factorial. ( Source )
#include <iostream>
using namespace std;
int main()
{
int num,limit,fact=1;
cout<<"enter limit";
cin>>limit;
num=limit;
while(num>=1)
{
fact=fact*num;
num--;
}
cout<<fact<<endl;
return 0;
}
23. By Jared
Made by Jared. ( Source )
#include <iostream>
using namespace std;
//Im not sure when this reaches stack overflow
int main() {
int input;
cin >> input;
float fact = input;
int e = 0;
for (int x = input; x > 2; x--)
{
fact *= (x-1);
//simplifies it to get past the stack overflow
while (fact>=10)
{
fact/=10;
e++;
}
}
cout << fact << "*10^" << e;
return 0;
}
24. By adrian valdiviez
Made by adrian valdiviez. ( Source )
#include <iostream>
using namespace std;
int main()
{
int num = 1;
int number;
int total = 1;
int x=0;
cin>>number;
while (num <= number) {
total *= (number-x);
num++;
x++;
}
cout << total << endl;
return 0;
}
25. By Rajdeep Deka
Made by Rajdeep Deka. ( Source )
#include <iostream>
using namespace std;
int factorial(int f)
{
if (f!=1)
{
return f*factorial(f-1);
}
else
{
return 1;
}
}
int main() {
int x;
cin >> x;
cout << factorial(x);
return 0;
}
26. By Daniele Compagnoni
Made by Daniele Compagnoni. ( Source )
#include <iostream>
using namespace std;
int evalFactorial(int n){
int r = 1;
for(int i = n; i > 0; i--){
r *= i;
}
return r;
}
int main()
{
int n;
cout << "Enter the number: " << endl;
cin >> n;
int r = evalFactorial(n);
cout << r << endl;
return 0;
}