Sum of Factorial Series
This program calculates the sum of factorials of first 5 numbers.
Problem Statement
Write a Java program that calculates the sum of factorials of first 5 numbers using for loop.
Source Code
| 1 | public class SumFactorialSeries { |
| 2 | public static void main(String[] args) { |
| 3 | int sum = 0; |
| 4 | for(int n=1; n<=5; n++) { |
| 5 | int fact = 1; |
| 6 | for(int i=1; i<=n; i++) { |
| 7 | fact *= i; |
| 8 | } |
| 9 | sum += fact; |
| 10 | } |
| 11 | System.out.println("Sum of factorials = " + sum + " - Eduinq"); |
| 12 | } |
| 13 | } |
Program Output
Sum of factorials = 153 - Eduinq
Explanation
The outer loop iterates numbers 1 to 5. For each number, the inner loop calculates factorial. The result is added to sum. This demonstrates nested loops for factorial series summation. Shows how nested loops can calculate sum of factorial series.