Write a program that calculates the factorial of a given number.
Program in C
C
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d:\n", n);
for (int i = 1; i <= n; i++) {
fact *= i;
printf("%d! = %d\n", i, fact);
}
return 0;
}
Explanation:
- The code starts by including the necessary header file stdio.h, which provides input and output functions. Then, the main() function is defined, which is the entry point of the program.
- Inside main(), two variables are declared: n and fact. n is used to store the user-entered positive integer, and fact is initialized to 1, which will be used to calculate the factorial.
- The program then prompts the user to enter a positive integer using printf(), and reads the input value using scanf(), storing it in n.
- Next, a for loop is used to calculate the factorial. The loop iterates from i = 1 to i <= n, incrementing i by 1 in each iteration.
- Inside the loop, fact is updated by multiplying it with i, and the current factorial value is printed using printf().
- Finally, the program reaches the end, and the main() function returns 0, indicating successful program execution.
Output:
Output
Enter a positive integer: 4
Factorial of 4:
1! = 1
2! = 2
3! = 6
4! = 24
Program in Java
Java
import java.util.Scanner;
public class FactorialJava {
public static void main(String[] args) {
int n, fact = 1;
System.out.print("Enter a positive integer: ");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
System.out.println("Factorial of " + n + ":");
for (int i = 1; i <= n; i++) {
fact *= i;
System.out.println(i + "! = " + fact);
}
scanner.close();
}
}
Explanation:
- import java.util.Scanner;: This line imports the Scanner class from the java.util package, allowing us to read user input from the console.
- public class FactorialJava: This line defines a public class named FactorialJava.
- public static void main(String[] args): This line is the entry point of the program. It declares the main method, which is where the execution of the program begins.
- int n, fact = 1;: This line declares two integer variables, n and fact. n will store the user input, and fact will store the factorial value. fact is initialized to 1 since it will be multiplied with numbers during the factorial calculation.
- System.out.print(“Enter a positive integer: “);: This line displays the message to the console, prompting the user to enter a positive integer.
- Scanner scanner = new Scanner(System.in);: This line creates a Scanner object named scanner, which allows us to read input from the console.
- n = scanner.nextInt();: This line reads an integer from the user and assigns it to the variable n.
- System.out.println(“Factorial of ” + n + “:”);: This line prints the message indicating that the factorial calculation is starting.
- for (int i = 1; i <= n; i++) {: This line begins a for loop that iterates from 1 up to the value of n. The loop counter is initialized as int i = 1, and the loop continues as long as i is less than or equal to n.
- fact *= i;: This line multiplies the current value of fact by the value of i and assigns the result back to fact. This calculation effectively computes the factorial.
- System.out.println(i + “! = ” + fact);: This line prints the factorial value for the current value of i. It concatenates the values of i, “! =”, and fact to form the output string.
- scanner.close();: This line closes the Scanner object to release system resources associated with it.
Output
Enter a positive integer: 4
Factorial of 4:
1! = 1
2! = 2
3! = 6
4! = 24