Factorial program in C, C++ and Java | Program to find the factorial of a number

Factorial program in C (with and without using command lines), C++ and other languages is discussed here. There are 4 methods to find the factorial of a number. They are:

  • Using tgamma function
  • Using for loop
  • Using recursion
  • Using functions

The solution for all these methods is explained below.

Explanation: Factorial of a non-negative integer n, denoted by n! It is the product of all positive integers less than or equal to n. For example,

factorial of a number

Program to find the factorial of a number using tgamma function

tgamma function is used to calculate factorial of a number. While using this function, you need to include the math.h header file. This function works only till 20!

#include<stdio.h>
#include<math.h>
int main()
{
int n, res;
printf("Enter the number : ");
scanf("%d", &n);
res = tgamma(n + 1);
printf("Factorial of the number %d is %d", n, res);
}
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n, res;
cout << "Enter the number : ";
cin >> n;
res = tgamma(n + 1);
cout << "Factorial of the number " << n << " is " << res;
}

Program to find the factorial of a number using functions

#include <stdio.h>
int factorial_of_a_number(int n)
{
int fact = 1, i;
if(n == 0)
return 1;
else
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
int main()
{
int n;
printf("Enter the number : ");
scanf("%d",&n);
if(n < 0)
printf("Invalid output");
else
printf("Factorial of the number %d is %d" ,n, factorial_of_a_number(n));
return 0;
}
#include <iostream>
using namespace std;
int factorial_of_a_number(int n)
{
int fact = 1, i;
if(n == 0)
return 1;
else
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
int main()
{
int n;
cout << "Enter the number : ";
cin >> n;
if(n < 0)
cout << "Invalid output";
else
cout << "Factorial of the number " << n << " is " << factorial_of_a_number(n);
return 0;
}
import java.util.Scanner;
public class Main
{
public static int factorial_of_a_number(int n)
{
int fact = 1, i;
if(n == 0)
return 1;
else
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter the number : ");
n = sc.nextInt();
if(n < 0)
System.out.println("Invalid output");
else
System.out.println("Factorial of the number " + n + " is " + factorial_of_a_number(n));
}
}
def factorial_of_the_number(n):
if(n == 0):
return 1
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact

n = int(input("Enter the number : "))
if(n < 0):
print ("Invalid input")
else:
print(factorial_of_the_number(n))

Program to find the factorial of a number using recursion

Factorial using recursion is easier and less complex.

#include <stdio.h>
int factorial_of_a_number(int n)
{
if(n == 0)
return 1;
else
return (n * factorial_of_a_number(n-1));
}
int main()
{
int n;
printf("Enter the number : ");
scanf("%d", &n);
if(n < 0)
printf("Invalid input");
else
printf("Factorial of the number %d is %d", n, factorial_of_a_number(n));
return 0;
}
#include <iostream>
using namespace std;
int factorial_of_a_number(int n)
{
if(n == 0)
return 1;
else
return (n * factorial_of_a_number(n-1));
}
int main()
{
int n;
cout << "Enter the number : ";
cin >> n;
if(n < 0)
cout << "Invalid input";
else
cout << "Factorial of the number " << n << " is " << factorial_of_a_number(n);
return 0;
}
import java.util.Scanner;
public class Main
{
public static int factorial_of_a_number(int n)
{
if(n == 0)
return 1;
else
return (n * factorial_of_a_number(n - 1));
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter the number : ");
n = sc.nextInt();
if(n < 0)
System.out.println("Invalid input");
else
System.out.println("Factorial of the number " + n + " is " + factorial_of_a_number(n));
}
}
def factorial_of_the_number(n):
if(n == 0):
return 1
fact = 1
return (n * factorial_of_the_number(n - 1))

n = int(input("Enter the number : "))
if(n < 0):
print ("Invalid input")
else:
print(factorial_of_the_number(n))

Program to find the factorial of a number using for loop and command line aruguments

#include<stdio.h>
int main(int a, char *b[])
{
int x, y, fact = 1, i;
x = atoi(b[1]); //atoi function is to convert a character to integer
for(i = 1; i <= x; i++)
{
fact = fact * i;
}
printf("%d", fact);
return 0;
}

Output:

factorial of a number