Python Program to find Sum of Even and Odd Numbers

Write a Python Program to find Sum of Even and Odd Numbers from 1 to N using For Loop with an example.

Python Program to find Sum of Even and Odd Numbers from 1 to N using For Loop

This Python program allows the user to enter the maximum limit value. Next, it is going to print even, and odd numbers from 1 to that user entered limit value. In this example, the For Loop makes sure that the number is between 1 and maximum limit value. Next, we used If Else to check the even number.

TIP: I suggest you refer Sum of Even and Sum of Odd numbers articles to understand the For Loop logic behind the sum of Even and odd numbers. And also refer Even or Odd Program and If Else to understand the Python logic

# Python Program to find Sum of Even and Odd Numbers from 1 to N
 
maximum = int(input(" Please Enter the Maximum Value : "))
even_total = 0
odd_total = 0
 
for number in range(1, maximum + 1):
    if(number % 2 == 0):
        even_total = even_total + number
    else:
        odd_total = odd_total + number
 
print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, even_total))
print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, odd_total))

Python Sum of Even and Odd Numbers using For Loop output

Python Program to find Sum of Even and Odd Numbers from 1 to N 1

Python Program to Calculate Sum of Even and Odd Numbers from 1 to N without If Statement

This Python sum of even and odd numbers program is the same as the above. However, this Python program allows users to enter Minimum and maximum value. Next, it prints even and odd numbers between Minimum and maximum value.

# Python Program to find Sum of Even and Odd Numbers from 1 to N

minimum = int(input(" Please Enter the Minimum Value : ")) 
maximum = int(input(" Please Enter the Maximum Value : "))

even_total = 0
odd_total = 0
 
for number in range(minimum, maximum + 1):
    if(number % 2 == 0):
        even_total = even_total + number
    else:
        odd_total = odd_total + number
 
print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, even_total))
print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, odd_total))
 Please Enter the Minimum Value : 10
 Please Enter the Maximum Value : 40
The Sum of Even Numbers from 1 to 40 = 400
The Sum of Odd Numbers from 1 to 40 = 375