Factorial Using While Loop

This program calculates factorial of a number using while loop.

Problem Statement

Write a Java program that calculates factorial of 5 using while loop.

Source Code

1 public class FactorialWhile {
2 public static void main(String[] args) {
3 int num = 5, fact = 1, i = 1;
4 while(i <= num) {
5 fact *= i;
6 i++;
7 }
8 System.out.println("Factorial of " + num + " = " + fact + " - Eduinq");
9 }
10 }

Program Output

Factorial of 5 = 120 - Eduinq

Explanation

The program initializes fact=1 and i=1. The while loop multiplies fact by i until i exceeds num. This demonstrates how while loops can calculate factorials. Shows how while loop can calculate factorial.

Quick Links to Explore