Python Program to Find Sum Of Odd and Even numbers From 1 to N

Python program to calculate sum of even and odd numbers from 1 to N; Through this tutorial, i am going to show you how to calculate and print sum of even and odd numbers from 1 to N (10, 100, 500, 100) using loop in python.

Python Program to Find Sum Of Odd and Even numbers From 1 to N

  • Python Program to Print Odd Numbers from 1 to N 
  • Python Program to Print Even Numbers from 1 to N

Python Program to Print Odd Numbers from 1 to N 

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

Output of the above program; as shown below:

Please Enter the Maximum Value : 15
1
3
5
7
9
11
13
15
The Sum of Odd Numbers from 1 to 15 = 64

Python Program to Print Even Numbers from 1 to N 

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

Output of the above program; as shown below:

Please Enter the Maximum Value : 8
2
4
6
8
The Sum of Even Numbers from 1 to 8 = 20

Recommended Python Tutorials

Recommended:-Python Map() Method

Be First to Comment

Leave a Reply

Your email address will not be published.