Factorial Program in C, C++, Java, JavaScript, and Python

Introduction

Factorial Program, Factorials are an essential concept in mathematics, commonly used in various mathematical and computational applications. A factorial of a non-negative integer n is the product of all positive integers from 1 to n. In this article, we will explore how to calculate the factorial of a given number using different programming languages. We will cover five programming languages: C, C++, Java, JavaScript, and Python.

Factorial Program | Understand using a six-step strategy

Step 1: Understand the Problem

Before we start writing code, let’s understand the problem. We need to calculate the factorial of a given non-negative integer.

Step 2: Design Test Cases

To ensure our code works correctly, let’s design test cases for different scenarios:

  1. Calculating the factorial of 5.
  2. Calculating a factorial of 0 (edge case).
  3. Calculating a factorial of 10.

Step 3: Derive the Solution (Pseudocode)

Let’s outline the solution in pseudocode before implementing it in code:

Function calculateFactorial(n):
    if n is 0:
        return 1

    Initialize factorial as 1
    Loop from i = 1 to n:
        Multiply factorial by i

    return factorial

Step 4: Test the Solution

Before implementing the solution in multiple programming languages, let’s manually verify the pseudocode against the test cases:

  • calculateFactorial(5) should return 120
  • calculateFactorial(0) should return 1
  • calculateFactorial(10) should return 3628800

Step 5: Write the Code

Factorial Program

Now, let’s implement the solution in various programming languages.

C
#include <stdio.h>

int calculateFactorial(int n) {
    if (n == 0) {
        return 1;
    }

    int factorial = 1;
    for (int i = 1; i <= n; i++) {
        factorial *= i;
    }
    return factorial;
}

int main() {
    int n = 5;
    printf("Factorial of %d is %d\n", n, calculateFactorial(n));
    return 0;
}
C

C++
#include <iostream>

int calculateFactorial(int n) {
    if (n == 0) {
        return 1;
    }

    int factorial = 1;
    for (int i = 1; i <= n; i++) {
        factorial *= i;
    }
    return factorial;
}

int main() {
    int n = 5;
    std::cout << "Factorial of " << n << " is " << calculateFactorial(n) << std::endl;
    return 0;
}
C++

Java
public class Factorial {
    public static int calculateFactorial(int n) {
        if (n == 0) {
            return 1;
        }

        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        return factorial;
    }

    public static void main(String[] args) {
        int n = 5;
        System.out.println("Factorial of " + n + " is " + calculateFactorial(n));
    }
}
Java

JavaScript
function calculateFactorial(n) {
    if (n === 0) {
        return 1;
    }

    let factorial = 1;
    for (let i = 1; i <= n; i++) {
        factorial *= i;
    }
    return factorial;
}

const n = 5;
console.log(`Factorial of ${n} is ${calculateFactorial(n)}`);
JavaScript

Python
def calculate_factorial(n):
    if n == 0:
        return 1

    factorial = 1
    for i in range(1, n + 1):
        factorial *= i
    return factorial

n = 5
print(f"Factorial of {n} is {calculate_factorial(n)}")
Python

Step 6: Test the Code

After implementing the code in each language, you can run the respective program for the test cases. Make sure the calculated factorials match your expectations based on the test cases.

By systematically approaching the problem, designing test cases, and implementing code in different programming languages, you can develop your coding skills and tackle various mathematical computations effectively.

Factorial Program Conclusion

The systematic problem-solving approach employed here demonstrates the importance of understanding the problem, creating meaningful test cases, developing clear pseudocode, implementing code, and rigorously testing it. By applying these practices, you can enhance your problem-solving skills and effectively tackle a wide range of programming challenges involving mathematical computations.



Leave a Comment