Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Function to calculate the factorial

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:

1. 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.
2. Inside the factorial function, an integer variable result is initialized to 1. This variable will store the factorial result.
3. 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.
4. After the loop finishes, the calculated factorial value is stored in result and returned from the function.
5. In the main function, an integer variable N is declared to store the user input.
6. The program prompts the user to enter a number using printf.
7. The value entered by the user is read and stored in N using scanf.
8. The factorial function is called with N as the argument, and the returned value is stored in an integer variable fact.
9. 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.
10. 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:

1. The Factorial class is defined, which contains two methods: factorial and main.
2. 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.
3. Inside the factorial method, an integer variable result is initialized to 1. This variable will store the factorial result.
4. 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.
5. After the loop finishes, the calculated factorial value is stored in result and returned from the method.
6. The main method is also static and serves as the entry point of the program.
7. Inside the main method, an integer variable N is declared to store the user input.
8. A Scanner object named scanner is created to read input from the user.
9. The program prompts the user to enter a number using System.out.print(“Enter a number: “).
10. The value entered by the user is read and stored in N using scanner.nextInt().
11. The factorial method is called with N as the argument, and the returned value is stored in an integer variable fact.
12. 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.
13. The program execution ends, and the program terminates.
Java Output
``````Enter a number: 4
The factorial of 4 is 24``````