Question:

Write a Java program to calculate and display the factorial of a number 5 using for loop.

Show Hint

Use a for loop to calculate factorials — multiply from 1 to the number.
Updated On: Jul 14, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

A factorial of a number is the product of all positive integers less than or equal to that number.
For example, the factorial of 5 is: 5 × 4 × 3 × 2 × 1 = 120.
In Java, this can be achieved using a for loop, where we multiply the number iteratively.
Below is the Java program to compute the factorial of 5:

This program initializes a variable factorial to 1 and multiplies it by each number from 1 to 5 using a for loop.
Finally, the result is printed on the screen.
 

Was this answer helpful?
0
0