Find the Factorial of a Number in C / C++ / Java / Python / C#

factorial

Programs to find the factorial of a number have been shown here. Both the iterative and recursive methods have been covered. The factorial of a number $n$ is denoted by $n!$. It is the product of all positive integers less than or equal to $n$. For example $5!$ can be represented as $5! = 5 \times 4 \times 3 \times 2 \times 1$. The algorithm, pseudocode and time complexity of the program have been shown below.






1. Algorithm to find factorial of a number


1. Take a number n as input.

2. Intialize variables f = 1 and i = 1

3. Multiply f and i and store the result inside f i.e. f = f * i

4. Increment i by 1 i.e. i = i + 1

5. Go to step 3 until i = n + 1

6. Print f as the result




2. Pseudocode to find factorial of a number


Input : A number $n$

Output : Factorial of the number $n$

1. Procedure factorial($n$):

2. $f \leftarrow 1$

3. Repear for $i = 1$ to $n$

4. $f \leftarrow f * i$

5. Return $f$

6. End Procedure





3. Time complexity to find factorial of a number


Time Complexity: O(n)

Where n is the given number.





4. Program & output to find factorial of a number using iteration

This section covers the C, C++, Java, Python and C# programs to find the factorial of a number using iteration.




4.1. C Program & output to find factorial of a number using iteration

Code has been copied
/*****************************************
            alphabetacoder.com
 C program to find factorial of a number
******************************************/
#include <stdio.h>

int main() {
    // declare variables
    int num, i;
    long fact = 1;

    // take input of the number 
    printf("Enter the number = ");
    scanf("%d", & num);

    // calculate the factorial
    for (i = 1; i <= num; i++)
        fact = fact * i;

    // display result
    printf("Factorial of %d is %ld", num, fact);

    return 0;
}

Output


Case 1:

Enter the number = 0

Factorial of 0 is 1


Case 2:

Enter the number = 5

Factorial of 5 is 120


Case 3:

Enter the number = 10

Factorial of 10 is 3628800




4.2. C++ Program & output to find factorial of a number using iteration

Code has been copied
/*****************************************
            alphabetacoder.com
 C++ program to find factorial of a number
******************************************/
#include <iostream>

using namespace std;

int main() {
    // declare variables
    int num, i;
    long fact = 1;

    // take input of the number 
    cout << "Enter the number = ";
    cin >> num;

    // calculate the factorial
    for (i = 1; i <= num; i++)
        fact = fact * i;

    // display result
    cout << "Factorial of " << num << " is " << fact;

    return 0;
}



4.3. Java Program & output to find factorial of a number using iteration

Code has been copied
/*****************************************
            alphabetacoder.com
Java program to find factorial of a number
******************************************/

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // declare variables
        int num, i;
        long fact = 1;

        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input of the number 
        System.out.print("Enter the number = ");
        num = sc.nextInt();

        // calculate the factorial
        for (i = 1; i <= num; i++)
            fact = fact * i;

        // display result
        System.out.println("Factorial of " + num + " is " + fact);
    }
}



4.4. Python Program & output to find factorial of a number using iteration

Code has been copied
#********************************************
#            alphabetacoder.com
#Python program to find factorial of a number
#********************************************

# take input of the number 
num = int(input("Enter the number = "))

# initialize
fact = 1

# calculate the factorial
for i in range(1, num + 1):
    fact = fact * i

# display result
print("Factorial of ",num, "is", fact)



4.5. C# Program & output to find factorial of a number using iteration

Code has been copied
/****************************************
        alphabetacoder.com
C# program to find factorial of a number
******************************************/

using System;

namespace Factorial {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int num, i;
            long fact = 1;

            // take input of the number 
            Console.Write("Enter the number = ");
            num = Convert.ToInt32(Console.ReadLine());

            // calculate the factorial
            for (i = 1; i <= num; i++) {
                fact = fact * i;
            }

            // display result
            Console.WriteLine("Factorial of " + num + " is " + fact);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}



5. Program & output to find factorial of a number using recursion

This section covers the C, C++, Java, Python and C# programs to find the factorial of a number using recursion.




5.1. C Program & output to find factorial of a number using recursion

Code has been copied
/****************************
    alphabetacoder.com
 C program to find factorial
 of a number using recursion
*****************************/
#include <stdio.h>

// recursive function to calculate factorial
long factorial(long n) {
    if (n == 0)	// exit condition
        return 1;
    else
        return n * factorial(n-1);
}

int main() {
    // declare variables
    int num;
    long fact = 1;

    // take input of the number 
    printf("Enter the number = ");
    scanf("%d", & num);
    
    // calculate factorial by calling the recursive function
    fact = factorial(num);
    // display result
    printf("Factorial of %d is %ld", num, fact);

    return 0;
}

Output


Case 1:

Enter the number = 0

Factorial of 0 is 1


Case 2:

Enter the number = 5

Factorial of 5 is 120


Case 3:

Enter the number = 10

Factorial of 10 is 3628800




5.2. C++ Program & output to find factorial of a number using recursion

Code has been copied
/*****************************
    alphabetacoder.com
 C++ program to find factorial
 of a number using recursion
******************************/
#include <iostream>

using namespace std;

// recursive function to calculate factorial
long factorial(long n) {
    if (n == 0) // exit condition
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    // declare variables
    int num;
    long fact = 1;

    // take input of the number 
    cout << "Enter the number = ";
    cin >> num;

    // calculate factorial by calling the recursive function
    fact = factorial(num);
    // display result
    cout << "Factorial of " << num << " is " << fact;

    return 0;
}



5.3. Java Program & output to find factorial of a number using recursion

Code has been copied
/*******************************
    alphabetacoder.com
 Java program to find factorial
 of a number using recursion
********************************/

import java.util.Scanner;

public class Main {
    // recursive function to calculate factorial
    public static long factorial(long n) {
        if (n == 0) // exit condition
            return 1;
        else
            return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        // declare variables
        int num;
        long fact;

        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input of the number 
        System.out.print("Enter the number = ");
        num = sc.nextInt();

        // calculate factorial by calling the recursive function
        fact = factorial(num);

        // display result
        System.out.println("Factorial of " + num + " is " + fact);
    }
}



5.4. Python Program & output to find factorial of a number using recursion

Code has been copied
#********************************************
#       alphabetacoder.com
# Python program to find factorial
# of a number using recursion
#********************************************

# recursive function to calculate factorial
def factorial(n):
    if n == 0:	# exit condition
        return 1
    else:
        return n * factorial(n-1)

def main():
    # take input of the number 
    num = int(input("Enter the number = "))
    
    # calculate factorial by calling the recursive function
    fact = factorial(num)

    # display result
    print("Factorial of ",num, "is",fact);

# driver code
main()



5.5. C# Program & output to find factorial of a number using recursion

Code has been copied
/***************************
    alphabetacoder.com
C# program to calculate
factorial using recursion
****************************/

using System;

namespace Factorial {
    class Program {
        // recursive function to calculate factorial
        public static long factorial(long n) {
            if (n == 0) // exit condition
                return 1;
            else
                return n * factorial(n - 1);
        }

        static void Main(string[] args) {
            // declare variables
            int num;
            long fact;

            // take input of the number 
            Console.Write("Enter the number = ");
            num = Convert.ToInt32(Console.ReadLine());

            // calculate factorial by calling the recursive function
            fact = factorial(num);

            // display result upto 2 decimal places
            Console.WriteLine("Factorial of " + num + " is " + fact);

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}