While Loop With Story as Example 1 -The Father's Savings

The Father's Savings: A Programming Story Here's a meaningful story that explains what this Java program does, making it more relatable: The Father's Savings Plan Once upon a time, there was a father who wanted to teach his child about saving money. He decided to follow a simple savings plan for 5 days: The Savings Rule: Day 1: Save ₹1 Day 2: Save ₹2 (Total = ₹1 + ₹2 = ₹3) Day 3: Save ₹3 (Total = ₹3 + ₹3 = ₹6) Day 4: Save ₹4 (Total = ₹6 + ₹4 = ₹10) Day 5: Save ₹5 (Total = ₹10 + ₹5 = ₹15) At the end of 5 days, the father would have saved a total of ₹15. How the Java Program Represents This Story public class Father { public static void main(String[] args) { int purse = 0; // Initial savings (empty purse) int day = 1; // Start from Day 1 while (day

Apr 9, 2025 - 15:55
 0
While Loop With Story as Example 1 -The Father's Savings

The Father's Savings: A Programming Story

Here's a meaningful story that explains what this Java program does, making it more relatable:

The Father's Savings Plan

Once upon a time, there was a father who wanted to teach his child about saving money. He decided to follow a simple savings plan for 5 days:

The Savings Rule:

  • Day 1: Save ₹1
  • Day 2: Save ₹2 (Total = ₹1 + ₹2 = ₹3)
  • Day 3: Save ₹3 (Total = ₹3 + ₹3 = ₹6)
  • Day 4: Save ₹4 (Total = ₹6 + ₹4 = ₹10)
  • Day 5: Save ₹5 (Total = ₹10 + ₹5 = ₹15)

At the end of 5 days, the father would have saved a total of ₹15.

How the Java Program Represents This Story

public class Father {
    public static void main(String[] args) {
        int purse = 0;   // Initial savings (empty purse)
        int day = 1;     // Start from Day 1

        while (day <= 5) {   // Repeat for 5 days
            purse = purse + day;  // Add day's savings to purse
            day = day + 1;       // Move to the next day
            System.out.println(purse); // Print current savings
        }
    }
}

Output Explanation

The program prints the cumulative savings after each day:

1   (Day 1: 0 + 1 = 1)
3   (Day 2: 1 + 2 = 3)
6   (Day 3: 3 + 3 = 6)
10  (Day 4: 6 + 4 = 10)
15  (Day 5: 10 + 5 = 15)

Key Learning Points

  1. Loop Concept (while loop) → Repeats for 5 days.
  2. Variable Update (purse = purse + day) → Adds money each day.
  3. Day Counter (day = day + 1) → Moves to the next day.
  4. Output Tracking (System.out.println) → Shows savings growth.

Real-Life Application

This is similar to:

  • Daily savings plans
  • Recurring deposits in a bank
  • Step-by-step progress tracking