Today’s challenge is to write a program that calculates the factorial of a number. This exercise will reinforce your understanding of loops and introduce you to recursive approaches in programming (if you’re feeling adventurous).
The factorial of a number n, denoted as n!, is the product of all positive integers from 1 to n.
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
Special cases:
Factorials are widely used in mathematics, particularly in combinatorics, probability, and algebra. For programmers, they are a great way to practice:
Factorial is a cumulative product. The factorial of nnn involves multiplying all integers from 1 to n:
You can solve this problem in multiple ways:
# Get input from the user
number = int(input("Enter a non-negative integer: "))
# Ensure the input is non-negative
if number < 0:
print("Factorial is not defined for negative numbers.")
else:
factorial = 1
for i in range(1, number + 1):
factorial *= i
print(f"The factorial of {number} is: {factorial}")
def factorial(n):
if n == 0 or n == 1: # Base case
return 1
else:
return n * factorial(n - 1) # Recursive call
# Get input from the user
number = int(input("Enter a non-negative integer: "))
# Ensure the input is non-negative
if number < 0:
print("Factorial is not defined for negative numbers.")
else:
print(f"The factorial of {number} is: {factorial(number)}")
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input from the user
System.out.print("Enter a non-negative integer: ");
int number = scanner.nextInt();
if (number < 0) {
System.out.println("Factorial is not defined for negative numbers.");
} else {
long factorial = 1; // Use long to handle large numbers
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("The factorial of " + number + " is: " + factorial);
}
}
}
public class Main {
public static long factorial(int n) {
if (n == 0 || n == 1) { // Base case
return 1;
}
return n * factorial(n - 1); // Recursive call
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input from the user
System.out.print("Enter a non-negative integer: ");
int number = scanner.nextInt();
if (number < 0) {
System.out.println("Factorial is not defined for negative numbers.");
} else {
System.out.println("The factorial of " + number + " is: " + factorial(number));
}
}
}
// Get input from the user
let number = parseInt(prompt("Enter a non-negative integer:"));
if (number < 0) {
console.log("Factorial is not defined for negative numbers.");
} else {
let factorial = 1;
for (let i = 1; i <= number; i++) {
factorial *= i;
}
console.log(`The factorial of ${number} is: ${factorial}`);
}
function factorial(n) {
if (n === 0 || n === 1) { // Base case
return 1;
}
return n * factorial(n - 1); // Recursive call
}
// Get input from the user
let number = parseInt(prompt("Enter a non-negative integer:"));
if (number < 0) {
console.log("Factorial is not defined for negative numbers.");
} else {
console.log(`The factorial of ${number} is: ${factorial(number)}`);
}
long
or libraries for big integers).Factorial calculations are not only a coding exercise but also a stepping stone to deeper concepts in algorithms and mathematics!
Prepare for Day 10: Prime Number Checker, where you’ll write a program to determine whether a number is prime — a great challenge to deepen your understanding of loops and conditions!