Python Simple Programs (using while loop) – Part 3

🔢 1. Compute Factorial of N

➜ This Python program calculates the factorial of a number using a while loop.

💻 Python Code
# Program to compute factorial of N n = int(input(“Enter value of N: “)) factorial = 1 i = 1 while i <= n: factorial = factorial * i i = i + 1 print(“Factorial of N is:”, factorial)
Factorial Concept
N! = N × (N − 1) × … × 2 × 1
For example: 5! = 5 × 4 × 3 × 2 × 1 = 120
💡 How It Works
1. The user enters the value of N.
2. factorial starts with 1 and i starts with 1.
3. The while loop continues as long as i <= n.
4. Each value of i is multiplied with factorial.
5. i = i + 1 moves the counter to the next value.
6. When i > n, the loop stops and the factorial is displayed.
🔎 Step-by-Step: N = 5
factorial = 1 × 1 = 1
factorial = 1 × 2 = 2
factorial = 2 × 3 = 6
factorial = 6 × 4 = 24
factorial = 24 × 5 = 120
⚠️ Important: Updating the Counter
Unlike a for loop, a while loop does not automatically update the counter.
i = i + 1
This statement is necessary to move toward the loop’s stopping condition. Without it, the loop could continue indefinitely.

▶ Sample Output

Enter value of N: 5
Factorial of N is: 120
📌 Quick Note: A while loop is useful when the number of repetitions depends on a condition. Always make sure that the loop condition can eventually become False.

➕ 2. Sum of First Five Natural Numbers

➜ This Python program uses a while loop to calculate the sum of the first five natural numbers.

💻 Python Code
# Program to find the sum of first five natural numbers sum = 0 count = 1 while count <= 5: sum = sum + count count = count + 1 print(“Sum of 1st 5 numbers is:”, sum)

▶ Sample Output

Sum of 1st 5 numbers is: 15
💡 How It Works
1. sum is initialized to 0.
2. count starts at 1.
3. The while loop runs while count <= 5.
4. Each value of count is added to sum.
5. count = count + 1 moves to the next number.
6. The loop stops when count becomes 6.
🔄 Three Important Parts of a while Loop
count = 1   → Initialization
while count <= 5:   → Condition
count = count + 1   → Update
🔎 Step-by-Step Calculation
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
⚠️ Important: A while loop requires the loop-control variable to be updated. Here, count = count + 1 ensures that count eventually becomes 6 and the loop terminates.

➕ 3. Sum of Integers from 1 to 100

➜ This Python program uses a while loop to calculate the sum of all integers from 1 to 100.

💻 Python Code
# Program to calculate the sum of integers from 1 to 100 sum = 0 i = 1 while i <= 100: sum = sum + i i = i + 1 print(“Sum of integers from 1 to 100 is:”, sum)

▶ Sample Output

Sum of integers from 1 to 100 is: 5050
💡 How It Works
1. sum is initialized to 0.
2. i starts at 1.
3. The loop runs while i <= 100.
4. Each value of i is added to sum.
5. i = i + 1 moves to the next integer.
6. When i becomes 101, the condition becomes false and the loop stops.
🔄 From for Loop to while Loop
The original for loop can be represented using three important parts in a while loop:
i = 1   → Initialization
while i <= 100:   → Condition
i = i + 1   → Update
🔎 What Happens in the Loop?
1 + 2 + 3 + 4 + 5 + … + 100
= 5050
⚠️ Important: In a while loop, the programmer must explicitly update the control variable. Here, i = i + 1 ensures that the loop eventually terminates.

➕ 4. Sum of First N Natural Numbers

➜ This Python program uses a while loop to calculate the sum of the first N natural numbers.

💻 Python Code
# Program to find the sum of first N natural numbers n = int(input(“Enter value of n: “)) sum = 0 i = 1 while i <= n: sum = sum + i i = i + 1 print(“Sum of n natural numbers is:”, sum)

▶ Sample Output

Enter value of n: 10
Sum of n natural numbers is: 55
💡 How It Works
1. The user enters the value of N.
2. sum is initialized to 0 and i starts at 1.
3. The loop continues while i <= n.
4. Each value of i is added to sum.
5. i = i + 1 moves the counter to the next number.
6. The loop stops when i becomes greater than N.
🔄 From for Loop to while Loop
The original for loop can be expressed using three explicit parts in a while loop:
i = 1   → Initialization
while i <= n:   → Condition
i = i + 1   → Update
🔎 For N = 10
1 + 2 + 3 + 4 + 5 + … + 10
= 55
📌 Quick Note: The value of N determines how many natural numbers are added. For example, if N = 10, the program adds the numbers from 1 to 10.

➕ 5. Sum of Even Numbers Using a while Loop

➜ This Python program uses a while loop to calculate the sum of all even numbers up to a given value of N.

💻 Python Code
# Program to find the sum of all even numbers up to N n = int(input(“Enter value of n: “)) sum = 0 i = 0 while i <= n: sum = sum + i i = i + 2 print(“Sum of even numbers up to n is:”, sum)

▶ Sample Output

Enter value of n: 10
Sum of even numbers up to n is: 30
💡 How It Works
1. The user enters the value of N.
2. sum starts at 0 and i starts at 0.
3. The while loop continues while i <= n.
4. The current even number is added to sum.
5. i = i + 2 moves directly to the next even number.
6. The loop stops when i becomes greater than N.
🔄 From for Loop to while Loop
In the original for loop, the step value 2 automatically moves to the next even number.
for i in range(0, n + 1, 2):
With a while loop, we perform that update ourselves:
i = i + 2
🔎 For N = 10
i takes the values:
0, 2, 4, 6, 8, 10
Therefore:
0 + 2 + 4 + 6 + 8 + 10 = 30
📌 Quick Note: The statement i = i + 2 is important because it skips the odd numbers and moves from one even number directly to the next. Without this update, the loop could become infinite.

◼ 6. Sum of Squares of First N Natural Numbers

➜ This Python program uses a while loop to calculate the sum of the squares of the first N natural numbers.

💻 Python Code
# Program to find the sum of squares of first N natural numbers n = int(input(“Enter value of n: “)) sum = 0 i = 1 while i <= n: sum = sum + i * i i = i + 1 print(“Sum of squares of integers up to n is:”, sum)

▶ Sample Output

Enter value of n: 5
Sum of squares of integers up to n is: 55
💡 How It Works
1. The user enters the value of N.
2. sum is initialized to 0 and i starts at 1.
3. The loop continues while i <= n.
4. i * i calculates the square of the current number.
5. The square is added to sum.
6. i = i + 1 moves the loop to the next natural number.
🔄 From for Loop to while Loop
In the original for loop, Python automatically moves i through the required values:
for i in range(1, n + 1):
With a while loop, the initialization, condition, and update are written explicitly:
i = 1   → Initialization
while i <= n:   → Condition
i = i + 1   → Update
🔎 For N = 5
1² + 2² + 3² + 4² + 5²
= 1 + 4 + 9 + 16 + 25
= 55
📌 Quick Note: The statement i = i + 1 is essential in this while loop. It moves i to the next natural number and ensures that the loop eventually terminates.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »