Write a Program to find the factorial of a given number in C , java , python

Algorithm

  1. Start with a variable to store the factorial value and set it to 1.
  2. Take the input of the number for which you want to find the factorial.
  3. Check if the number is negative. If it is, display an error message as the factorial is not defined for negative numbers.
  4. If the number is non-negative, proceed with the following steps:
    • Initialize a loop variable to 1.
    • Enter a loop that iterates from 1 to the given number.
    • Multiply the current factorial value by the loop variable.
    • Update the factorial value with the result of multiplication.
    • Increment the loop variable by 1.
    • Repeat the above four steps until the loop variable reaches the given number.
  5. After the loop completes, the factorial value will hold the factorial of the given number.
  6. Display the factorial value as the output.
factorial = 1
input number
if number < 0:
    display "Factorial is not defined for negative numbers."
else:
    loop_var = 1
    while loop_var <= number:
        factorial = factorial * loop_var
        increment loop_var by 1
    display factorial

C program

#include <stdio.h>

int factorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        int result = factorial(num);
        printf("Factorial of %d is %d.\n", num, result);
    }

    return 0;
}

In this program, the factorial() function is defined recursively. It checks if the number n is either 0 or 1 (base case), in which case it returns 1. Otherwise, it calls itself with n-1 and multiplies the current number n with the factorial of the smaller value. The function continues this process until it reaches the base case and returns the final factorial value.

C++

#include <iostream>

unsigned long long factorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    else
        return n * factorial(n - 1);
}

int main() {
    int num;
    std::cout << "Enter a number: ";
    std::cin >> num;

    if (num < 0) {
        std::cout << "Factorial is not defined for negative numbers." << std::endl;
    } else {
        unsigned long long result = factorial(num);
        std::cout << "Factorial of " << num << " is " << result << "." << std::endl;
    }

    return 0;
}

In this program, we have a recursive function factorial() that calculates the factorial of a given number. The main() function prompts the user to enter a number using std::cout and std::cin, and then calls the factorial() function to compute the factorial. The result is then displayed on the console using std::cout.

Please note that the factorial() function assumes that the input number is a non-negative integer. Also, an unsigned long long data type is used to handle larger factorial values to avoid overflow.

JAVA

import java.util.Scanner;

public class Factorial {
    public static int factorial(int n) {
        if (n == 0 || n == 1)
            return 1;
        else
            return n * factorial(n - 1);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        if (num < 0) {
            System.out.println("Factorial is not defined for negative numbers.");
        } else {
            int result = factorial(num);
            System.out.println("Factorial of " + num + " is " + result + ".");
        }
    }
}

In this program, we have a recursive method factorial() that calculates the factorial of a given number. The main() method prompts the user to enter a number, reads it from the standard input using the Scanner class, and then calls the factorial() method to compute the factorial. The result is then displayed on the console.

Please note that the factorial() method assumes that the input number is a non-negative integer.

Python

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

num = int(input("Enter a number: "))

if num < 0:
    print("Factorial is not defined for negative numbers.")
else:
    result = factorial(num)
    print(f"Factorial of {num} is {result}.")

In this program, we define a recursive function factorial() that calculates the factorial of a given number. The input() function is used to prompt the user to enter a number, and the int() function is used to convert the input to an integer. Then, we check if the number is negative. If it is, we display a message indicating that the factorial is not defined for negative numbers. Otherwise, we call the factorial() function to compute the factorial and display the result using f-string formatting.

Please note that the factorial() function assumes that the input number is a non-negative integer.

Scroll to Top