Factorial Sum with While Loop

This program calculates sum of factorials from 1! to 5! using while loop.

Problem Statement

Write a Java program that calculates sum of factorials from 1! to 5! using while loop.

Source Code

1 public class FactorialSumWhile {
2 public static void main(String[] args) {
3 int n = 5, sum = 0, k = 1;
4 while(k <= n) {
5 int fact = 1;
6 for(int i=1; i<=k; i++) {
7 fact *= i;
8 }
9 sum += fact;
10 k++;
11 }
12 System.out.println("Sum of factorials = " + sum + " - Eduinq");
13 }
14 }

Program Output

Sum of factorials = 153 - Eduinq

Explanation

The while loop iterates numbers 1 to 5, inner for loop calculates factorial, and result is added to sum. Shows how while loop can calculate factorial sum series.

Quick Links to Explore