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 3 methods to find the factorial of a number. They are:

  • 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 functions

#include <stdio.h>

int factorial_of_a_number(int n)
{
if(n == 0)
return 1;
if(n < 0)
printf(“Invalid input\n”);
int fact = 1, i;
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}

int main()
{
int n;
printf(“\nEnter the number : “);
scanf(“%d”,&n);
printf(“\nFactorial of the number %d is %d” ,n, factorial_of_a_number(n));
printf(“\n”);
return 0;
}

#include <iostream>
using namespace std;

int factorial_of_a_number(int n)
{
if(n == 0)
return 1;
if(n < 0)
cout << “Invalid input\n”;0
int fact = 1, i;
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}

int main()
{
int n;
cout << “\nEnter the number : “;
cin >> n;
cout << “\nFactorial of the number ” << n << ” is ” << factorial_of_a_number(n);
cout << endl;
return 0;
}

import java.util.*;
public class Main
{
static int factorial_of_the_number(int n)
{
if(n < 0)
return -1;
if(n == 0)
return 1;
int fact = 1, i;
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
public static void main(String[] args)
{

int n;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the number : “);
n = sc.nextInt();
System.out.println(“Factorial of the number ” + n + ” is “+factorial_of_the_number(n));
}
}

def factorial_of_the_number(n):
    if(n < 0):
        return -1
    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 : “))
print(“Factorial of the number : “,end=””)
print(factorial_of_the_number(n))

Program to find the factorial of a number using recursion

Factorial using recursion is easier and less complex.

// Factorial of a number in C using recursion

#include <stdio.h>

int factorial_of_a_number(int n)
{
if(n < 0)
return -1;
if(n == 0)
return 1;
else
return (n * factorial_of_a_number(n-1));
}

int main()
{
int n;
printf(“\nEnter the number : “);
scanf(“%d”,&n);
printf(“\nFactorial of the number %d is %d\n”,n,factorial_of_a_number(n));
return 0;
}

// Factorial of a number in C++ using recursion

#include <iostream>
using namespace std;

int factorial_of_a_number(int n)
{
if(n < 0)
return -1;
if(n == 0)
return 1;
else
return (n * factorial_of_a_number(n-1));
}

int main()
{
int n;
cout << “\nEnter the number : “;
cin >> n;
cout << “\nFactorial of the number ” << n << ” is ” << factorial_of_a_number(n);
cout << endl;
return 0;
}

// Factorial of a number in java using recursion

import java.util.*;
public class Main
{
static int factorial_of_the_number(int n)
{
if(n < 0)
return -1;
if(n == 0)
return 1;
else
return (n * factorial_of_the_number(n – 1));
}
public static void main(String[] args)
{

int n;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the number : “);
n = sc.nextInt();
System.out.println(“Factorial of the number ” + n + ” is “+factorial_of_the_number(n));
}
}

 

 

def factorial_of_the_number(n):
    if(n < 0):
        return -1
    if(n == 0):
        return 1
    else:
        return (n * factorial_of_the_number(n – 1))

n = int(input(“Enter the number : “))
print(“Factorial of the number : “,end=””)
print(factorial_of_the_number(n))

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

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

Output:

factorial of a number