Program For Factorial of a Number

Generally factorial of a number of a non-negative integer is the multiplication of all integers smaller than or equal to n. For example, Factorial of 6 is 6*5*4*3*2*1 which is 720.

Program For Factorial Of A Number
Program For Factorial Of A Number

Recursive Solution:

Factorial can be calculated by following recursive formula.

  n! = n * (n-1)!
  n! = 1 if n = 0 or n = 1 

C++ program to find factorial of a given no.

// C++ program to find factorial of given number
#include <iostream>
using namespace std;

// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
	if (n == 0)
		return 1;
	return n * factorial(n - 1);
}

// Driver code
int main()
{
	int num = 5;
	cout << "Factorial of "
		<< num << " is " << factorial(num) << endl;
	return 0;
}

// This code is contributed by Shivi_Aggarwal

C program to find factorial of a given no.

// C program to find factorial of given number
#include <stdio.h>

// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
	if (n == 0)
		return 1;
	return n * factorial(n - 1);
}

int main()
{
	int num = 5;
	printf("Factorial of %d is %d", num, factorial(num));
	return 0;
}

Java program to find factorial of a given no.

// Java program to find factorial of given number
class Test {
	// method to find factorial of given number
	static int factorial(int n)
	{
		if (n == 0)
			return 1;

		return n * factorial(n - 1);
	}

	// Driver method
	public static void main(String[] args)
	{
		int num = 5;
		System.out.println(
			"Factorial of " + num
			+ " is " + factorial(5));
	}
}

Python program to find factorial of a given no.

# Python 3 program to find
# factorial of given number

# Function to find factorial of given number
def factorial(n):
	
	if n == 0:
		return 1
	
	return n * factorial(n-1)

# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))

# This code is contributed by Smitha Dinesh Semwal

C# program to find factorial of a given no.

// C# program to find factorial
// of given number
using System;

class Test {
	// method to find factorial
	// of given number
	static int factorial(int n)
	{
		if (n == 0)
			return 1;

		return n * factorial(n - 1);
	}

	// Driver method
	public static void Main()
	{
		int num = 5;
		Console.WriteLine("Factorial of "
						+ num + " is " + factorial(5));
	}
}

// This code is contributed by vt_m

PHP program to find factorial of a given no.

<?php
// PHP program to find factorial
// of given number

// function to find factorial
// of given number
function factorial($n)
{
	if ($n == 0)
		return 1;
	return $n * factorial($n - 1);
}

	// Driver Code
	$num = 5;
	echo "Factorial of ", $num, " is ", factorial($num);

// This code is contributed by m_kit
?>

Output:

Factorial of 5 is 120

If you find any problem with this article please WhatsApp Us. And if the article is helpful for you please share on social media.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 190

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply