Write a function that calculates the factorial of a given number N and returns the result.
Program in C
C
#include <stdio.h>
int factorial(int N) {
int result = 1;
// Calculate factorial
for (int i = 1; i <= N; i++) {
result =result* i;
}
return result;
}
int main() {
int N;
// Getting input from the user
printf("Enter a number: ");
scanf("%d", &N);
// Calculate factorial and display the result
int fact = factorial(N);
printf("The factorial of %d is %d\n", N, fact);
return 0;
}
Explanation:
- The factorial function is defined, which takes an integer N as a parameter and returns an integer result. This function calculates the factorial of the given number N.
- Inside the factorial function, an integer variable result is initialized to 1. This variable will store the factorial result.
- A for loop is used to calculate the factorial. The loop starts from 1 and iterates up to N. In each iteration, the value of i is multiplied with the current value of result and the updated value is assigned back to result.
- After the loop finishes, the calculated factorial value is stored in result and returned from the function.
- In the main function, an integer variable N is declared to store the user input.
- The program prompts the user to enter a number using printf.
- The value entered by the user is read and stored in N using scanf.
- The factorial function is called with N as the argument, and the returned value is stored in an integer variable fact.
- Finally, the program displays the calculated factorial by printing the message “The factorial of %d is %d\n” using printf. The placeholders %d are replaced with the values of N and fact, respectively.
- The program execution ends, and the program terminates.
C Output
Enter a number: 5
The factorial of 5 is 120
Program in Java
Java
import java.util.Scanner;
public class Factorial {
public static int factorial(int N) {
int result = 1;
// Calculate factorial
for (int i = 1; i <= N; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
int N;
// Getting input from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
N = scanner.nextInt();
// Calculate factorial and display the result
int fact = factorial(N);
System.out.printf("The factorial of %d is %d\n", N, fact);
}
}
Explanation:
- The Factorial class is defined, which contains two methods: factorial and main.
- The factorial method is a static method that takes an integer N as a parameter and returns an integer result. This method calculates the factorial of the given number N.
- Inside the factorial method, an integer variable result is initialized to 1. This variable will store the factorial result.
- A for loop is used to calculate the factorial. The loop starts from 1 and iterates up to N. In each iteration, the value of i is multiplied with the current value of result using the *= operator, and the updated value is assigned back to result.
- After the loop finishes, the calculated factorial value is stored in result and returned from the method.
- The main method is also static and serves as the entry point of the program.
- Inside the main method, an integer variable N is declared to store the user input.
- A Scanner object named scanner is created to read input from the user.
- The program prompts the user to enter a number using System.out.print(“Enter a number: “).
- The value entered by the user is read and stored in N using scanner.nextInt().
- The factorial method is called with N as the argument, and the returned value is stored in an integer variable fact.
- Finally, the program displays the calculated factorial by printing the message “The factorial of %d is %d\n” using System.out.printf(). The placeholders %d are replaced with the values of N and fact, respectively.
- The program execution ends, and the program terminates.
Java Output
Enter a number: 4
The factorial of 4 is 24