• Post author:
• Post category:C Programs

## What is factorial of a number?

Factorial program in c c++ and java : Factorial number is the product of positive descending integers. Factorial of a number is denoted by n!

Note: Factotial of zero is one and factorial for a negative number doesnot exists.

### Example of a factorial number:

`4! = 4*3*2*1 = 24`

`3! = 3*2*1 = 6`

In mathematics basically factorial is used in permutaion and combinations i.e. nPr and nCr.

## Pre-requisite Programming concepts:

• Data-types in C, C++ and Java
• Input output operations in C, C++ and Java
• Addition and Multiplication Operator in C, C++ and Java
• For loop in C, C++ and Java

## Logic for factorial program in C, C++ and Java

• Just take the input of a number for which you want to calculate the factorial.
• Define factorial variable and assign 1 to it. Choose the datatype as double or long long int in case of C/C++
• Loop from one to that number. Better use for loop.
• Update the product of the factorial and index variable for each iteration.
• When the for loop exits then we will have the factorial value stored in that variable

## Factorial program in C

How to write a factorial program in c ?

Below is the c program to find factorial of a number

``````//c program for factorial of a number
#include<stdio.h>
int main(int argc, char const *argv[])
{
int num;
printf( "Factorial program in c\nEnter a number to find factorial\n" );
scanf("%d",&num);
long long factorial=1;
//calculate factorial
for (int i=1 ; i<=num ; i++ )
{
factorial=factorial*i;
}
printf("\nFactorial of %d is : %lld ",num,factorial);
return 0;
}``````

## Factorial program using recursion in c

``````//Factorial program using recursion in c
#include <stdio.h>
int find_factorial(int);
int main(void) {
int n,f;
printf("\nEnter number to find factorial\n");
scanf("%d", &n);
factorial = find_fact(n);
printf("%d", factorial);
return 0;
}

int find_fact(int n){
return n > 1 ? n * fact(n - 1) : 1;
}``````

## Factorial program in C++

Below is the factorial program in c++ programming language.

``````#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int num;
cout<<"Factorial program in c\nEnter a number to find factorial\n";
cin>>num;
long long factorial=1;
//calculate factorial
for (int i=1 ; i<=num ; i++ )
{
factorial=factorial*i;
}
cout<<"\nFactorial of"<<num<<" is : "<<factorial<<endl;
return 0;
}``````

## Factorial program using recursion in c++

``````//Factorial program using recursion in c++
#include <iostream>
using namespace std;
int fact(int);
int main(void) {
int n,f;
cout<<"\nEnter number to find factorial\n";
cin>>n;
factorial = fact(n);
cout<<"Factorial is :<< factorial;
return 0;
}

int fact(int n){
return n > 1 ? n * fact(n - 1) : 1;
}``````

## Factorial program in java

Below is the factorial program in java using scanner.

``````import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
int number;
System.out.println("Java program to find factorial of a number");
//read the number using scanner class
System.out.println("Enter a number to find its factorial:");
Scanner stdin=new Scanner(System.in);
number=stdin.nextInt();
long  factorial=1;
//finds factorial
for(int i=1; i<=number ;  i++)
{
factorial=factorial*i;
}
System.out.println("Factorial of "+number+" is "+factorial);
}

}``````

## Factorial program in java using while loop

``````//factorial program in java using while loop

import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
int number;
System.out.println("Java program to find factorial of a number");
//read the number using scanner class
System.out.println("Enter a number to find its factorial:");
Scanner stdin=new Scanner(System.in);
number=stdin.nextInt();
long  factorial=1;
//finds factorial
int i=1;
while(i<=number)
{
factorial=factorial*i;
i++;
}
System.out.println("Factorial of "+number+" is "+factorial);
}

}``````

## Factorial program using recursion in java

How to write factorial program in java?

``````import java.util.Scanner;
public class factorial {
static int find_factorial(int n)
{
return n > 1 ? n * find_factorial(n - 1) : 1;
}

public static void main(String[] args) {
int number;
System.out.println("Java program to find factorial of a number");
//read the number using scanner class
System.out.println("Enter a number to find its factorial:");
Scanner stdin=new Scanner(System.in);
number=stdin.nextInt();
System.out.println("Factorial of "+number+" is "+find_factorial(n));
}

}``````