You can replace the `number` variable with the desired value for which you want to find the factorial, and the program will calculate it using each of the four methods.
Certainly! Here's a Python program to find the factorial of a number using four different methods, including recursion, a while loop, a for loop, and a mathematical formula:
# Method 1: Using Recursion
def factorial_recursive(n):
if n == 0:
return 1
else :
return n * factorial_recursive(n - 1)
# Method 2: Using a While Loop
def factorial_while_loop(n):
result = 1
while n > 0:
result *= n
n -= 1
return result
# Method 3: Using a For Loop
def factorial_for_loop(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
# Method 4: Using a Mathematical Formula
import math
def factorial_math_formula(n):
return math.factorial(n)
# Input from the user
number = int(input("Enter a non-negative integer: "))
# Check if the input is a non-negative integer
if number < 0:
print("Factorial is not defined for negative numbers.")
else:
# Calculate and display the factorial using each method
print(f"Factorial using Recursion
{factorial_recursive(number)}")
print(f"Factorial using While Loop: {factorial_while_loop(number)}")
print(f"Factorial using For Loop: {factorial_for_loop(number)}")
print(f"Factorial using
This program defines four functions to calculate the factorial using different methods and then prompts the user to enter a non-negative integer to find its factorial using each of these methods.
Answers & Comments
Answer:
Sure, here's a Java program to find the factorial of a number using four different methods:
```java
public class FactorialCalculator {
// Method 1: Using a recursive method
public static long factorialRecursive(int n) {
if (n == 0) {
return 1;
} else {
return n * factorialRecursive(n - 1);
}
}
// Method 2: Using a while loop
public static long factorialIterative(int n) {
long result = 1;
while (n > 0) {
result *= n;
n--;
}
return result;
}
// Method 3: Using a for loop
public static long factorialUsingForLoop(int n) {
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// Method 4: Using Java 8 Stream
public static long factorialUsingStream(int n) {
return LongStream.rangeClosed(1, n).reduce(1, (a, b) -> a * b);
}
public static void main(String[] args) {
int number = 5; // Replace this with the number for which you want to calculate the factorial
System.out.println("Factorial (Recursive): " + factorialRecursive(number));
System.out.println("Factorial (Iterative): " + factorialIterative(number));
System.out.println("Factorial (Using For Loop): " + factorialUsingForLoop(number));
System.out.println("Factorial (Using Stream): " + factorialUsingStream(number));
}
}
```
You can replace the `number` variable with the desired value for which you want to find the factorial, and the program will calculate it using each of the four methods.
Answer:
Certainly! Here's a Python program to find the factorial of a number using four different methods, including recursion, a while loop, a for loop, and a mathematical formula:
# Method 1: Using Recursion
def factorial_recursive(n):
if n == 0:
return 1
else :
return n * factorial_recursive(n - 1)
# Method 2: Using a While Loop
def factorial_while_loop(n):
result = 1
while n > 0:
result *= n
n -= 1
return result
# Method 3: Using a For Loop
def factorial_for_loop(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
# Method 4: Using a Mathematical Formula
import math
def factorial_math_formula(n):
return math.factorial(n)
# Input from the user
number = int(input("Enter a non-negative integer: "))
# Check if the input is a non-negative integer
if number < 0:
print("Factorial is not defined for negative numbers.")
else:
# Calculate and display the factorial using each method
print(f"Factorial using Recursion
{factorial_recursive(number)}")
print(f"Factorial using While Loop: {factorial_while_loop(number)}")
print(f"Factorial using For Loop: {factorial_for_loop(number)}")
print(f"Factorial using
This program defines four functions to calculate the factorial using different methods and then prompts the user to enter a non-negative integer to find its factorial using each of these methods.