Python Simple Programs (using for loop) – Part 2

🔢 1. Compute Factorial of N

➜ This Python program calculates the factorial of a positive integer using a for loop.

💻 Python Code
# Program to compute factorial of N n = int(input(“Enter value of N: “)) factorial = 1 for i in range(1, n + 1): factorial = factorial * i print(“Factorial of N is:”, factorial)
Factorial Formula
N! = N × (N − 1) × (N − 2) × … × 2 × 1
For example, 5! = 5 × 4 × 3 × 2 × 1 = 120.
💡 How the Program Works
1. The user enters a positive integer N.
2. factorial is initialized to 1.
3. The for loop runs from 1 to N.
4. Each value of i is multiplied into factorial.
5. The final factorial value is displayed using print().
🔎 Example: N = 5
factorial = 1
1 × 2 × 3 × 4 × 5 = 120

▶ Sample Output

Enter value of N: 5
Factorial of N is: 120
📌 Quick Note: The factorial of 0 is also defined as 0! = 1. In this program, the initial value factorial = 1 supports that definition.

➕ 2. Sum of First Five Natural Numbers

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

💻 Python Code
# Program to find the sum of first five natural numbers sum = 0 for count in range(1, 6): sum = sum + count print(“Sum of 1st 5 numbers is:”, sum)
How the Sum Builds Up
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
💡 How the Program Works
1. sum is initialized to 0.
2. range(1, 6) generates the numbers 1 through 5.
3. Each number is added to sum.
4. print() displays the final sum.

▶ Sample Output

Sum of 1st 5 numbers is: 15
📌 Quick Note: In range(1, 6), the starting value is included, but the ending value 6 is excluded. Therefore, the loop runs for 1, 2, 3, 4, 5.

➕ 3. Sum of Integers from 1 to 100

➜ This Python program uses a for 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 for i in range(1, 101): sum = sum + i print(“Sum of integers from 1 to 100 is:”, sum)
🔢 Key Concept
The variable sum acts as an accumulator. Each number generated by the loop is added to it.
sum = sum + i
💡 How the Program Works
1. sum starts with the value 0.
2. range(1, 101) generates numbers from 1 through 100.
3. Each number is added to sum.
4. After the loop finishes, sum contains the final result.
🔎 Why range(1, 101)?
In range(1, 101), 1 is included but 101 is excluded. Therefore, the loop processes 1 to 100.

▶ Sample Output

Sum of integers from 1 to 100 is: 5050
📌 Quick Note: The program uses repeated addition. The final result is 5050. The same accumulator technique can be used to calculate the sum of other ranges of numbers.

➕ 4. Sum of Integers from 1 to 100

➜ This Python program uses a for 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 for i in range(1, 101): sum = sum + i print(“Sum of integers from 1 to 100 is:”, sum)
🔢 Key Concept
The variable sum acts as an accumulator. Each number generated by the loop is added to it.
sum = sum + i
💡 How the Program Works
1. sum starts with the value 0.
2. range(1, 101) generates numbers from 1 through 100.
3. Each number is added to sum.
4. After the loop finishes, sum contains the final result.
🔎 Why range(1, 101)?
In range(1, 101), 1 is included but 101 is excluded. Therefore, the loop processes 1 to 100.

▶ Sample Output

Sum of integers from 1 to 100 is: 5050
📌 Quick Note: The program uses repeated addition. The final result is 5050. The same accumulator technique can be used to calculate the sum of other ranges of numbers.

🔢 5. Sum of Squares of N Natural Numbers

➜ This Python program calculates the sum of the squares of the first N natural numbers using a for loop.

💻 Python Code
# Program to find the sum of squares of first N natural numbers n = int(input(“Enter value of n: “)) sum = 0 for i in range(1, n + 1): sum = sum + i * i print(“Sum of squares of integers up to n is:”, sum)
📐 Concept
1² + 2² + 3² + … + N²
For N = 5: 1² + 2² + 3² + 4² + 5² = 55
💡 How the Program Works
1. The user enters the value of N.
2. sum is initialized to 0.
3. range(1, n + 1) generates the numbers from 1 to N.
4. Each number is squared using i * i and added to sum.
5. The final sum is displayed using print().
🔎 Example: N = 5
1² + 2² + 3² + 4² + 5²
= 1 + 4 + 9 + 16 + 25 = 55

▶ Sample Output

Enter value of n: 5
Sum of squares of integers up to n is: 55
📌 Quick Note: The expression i * i calculates the square of each number. The variable sum keeps a running total throughout the loop.

➕ 6. Sum of All Even Numbers up to N

➜ This Python program uses a for 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 for count in range(0, n + 1, 2): sum = sum + count print(“Sum of even numbers up to n is:”, sum)
🔢 Understanding range()
range(0, n + 1, 2)
0 is the starting value, n + 1 sets the upper limit, and 2 is the step value.
For N = 10, the loop generates: 0, 2, 4, 6, 8, 10.
💡 How the Program Works
1. The user enters the value of N.
2. sum is initialized to 0.
3. range(0, n + 1, 2) generates even numbers.
4. Each even number is added to sum.
5. The final sum is displayed using print().
🔎 Example: N = 10
0 + 2 + 4 + 6 + 8 + 10 = 30
Since adding 0 does not change the result, the sum is 30.

▶ Sample Output

Enter value of n: 10
Sum of even numbers up to n is: 30
📌 Quick Note: The third argument of range() is the step. Using a step of 2 lets the loop move directly from one even number to the next.

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 »