When we start learning programming, many people jump directly into writing code. But good programmers first try to understand the logic behind the problem.
In this blog, we will solve two interesting problems:
- Sum of
nnumbers - A frog escaping from a 50-feet well
Instead of writing code immediately, we will first think like humans and build the logic step by step.
1. Sum of N Numbers Using While Loop
Suppose we want to find the sum from 1 to n.
Example:
1 + 2 + 3 + 4 + 5 = 15
Step 1: Understand the Logic
Before coding, think carefully.
We need to:
- Start from number
1 - Keep adding numbers one by one
- Stop when we reach
n
We will use:
- one variable for counting numbers
- one variable for storing the total sum
Step 2: Dry Run
Suppose n = 5
| Current Number | Total Sum |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
| 4 | 10 |
| 5 | 15 |
Now the logic becomes easy.
Python Code Using While Loop
n = int(input("Enter a number: "))
i = 1
total = 0
while i <= n:
total = total + i
i = i + 1
print("Sum is:", total)
Explanation of the Code
i = 1
We start counting from 1.
total = 0
This variable stores the final sum.
Initially, sum is zero.
while i <= n
The loop runs until i becomes greater than n.
total = total + i
This line adds the current number into the total.
Example:
total = 0 + 1
total = 1 + 2
total = 3 + 3
and so on.
i = i + 1
This increases the value of i by 1 every time.
Without this line, the loop would run forever.
Output
Enter a number: 5
Sum is: 15
2. Frog Story – Escaping From the Well
Now let’s solve a famous logic problem.
The Story
A frog falls into a well that is:
50 feet deep
Every day:
- Frog climbs up
2 feet - At night, it slips down
1.25 feet
Question:
In how many days will the frog escape?
Step 1: Think About the Logic
First understand what happens in one complete day.
During the day:
+2 feet
At night:
-1.25 feet
So actual progress in one full day is:
2 - 1.25 = 0.75 feet
This means the frog slowly moves upward.
Code
feet = 50
up = 2
down = 1.25
day = 0
while feet > 0:
feet = feet - up + down
day = day + 1
print(day)
Proper Explanation of This Code
At first glance, this code may look confusing because:
feet starts from 50
Here, feet does not represent how much the frog climbed.
Instead, it represents:
how much distance is still left
Initially:
feet = 50
means:
50 feet are remaining to escape
Understanding the Main Logic
Inside the loop:
feet = feet - up + down
This means:
- subtract the upward climb
- add the downward slip
So every day:
remaining distance decreases by 0.75 feet
because:
2 - 1.25 = 0.75
Day-by-Day Understanding
Day 1
Remaining distance:
50 - 2 + 1.25 = 49.25
Day 2
49.25 - 2 + 1.25 = 48.5
The remaining distance keeps decreasing slowly.
day = day + 1
This line counts the number of days.
Each loop means:
one full day completed
while feet > 0
The loop runs until the remaining distance becomes zero or negative.
That means:
frog has escaped
Final Output
67
Important Note About This Logic
This code is mathematically correct according to the loop logic.
But in real-life logic:
- the frog escapes immediately when it reaches the top
- it should not slip back on the final day
So the more realistic answer is:
65 days
But the given code treats every day as:
climb first and slip every single time
That is why the output becomes slightly different.
Conclusion
Programming is not only about syntax and coding.
Real programming starts with thinking.
Whether it is finding the sum of numbers or helping a frog escape a well, logic always comes first.
So next time you solve a problem:
Think first, code later.
That is the mindset of a good programmer.
Top comments (1)
The frog escape logic in your example is a neat way to think step-by-step, but the day-by-day math should focus more on practical outcomes, like the exact day the frog reaches the top. Many prep materials skip these details. I've been using prachub.com to find similar practical coding banks, and their system design and DSA resources are surprisingly close to what I've seen in actual interviews. It's better than piecing together threads on Glassdoor.