Find elements

Python program to calculate sum of odd and even numbers

Python program to calculate sum of odd and even numbers

In this tutorial, we will discuss The Python program to calculate sum of odd and even numbers

In this article, we are going to learn the concept of how to  calculate the sum of odd and even numbers in the Python language

 

What is even or odd numbers?

When any integer value which ends in 0,2,4,6,8 is divided by two it is called as an even number

Example for even numbers : 34,-62,58,890

 

When any integer value which ends in 0,1,3,5,7,9 is not divided by two it is called as an odd number

Example for  odd numbers : 21,567,-57,67

 

We can use a modular operator to find odd or even number in the given range.

if n%2==0,  n is an even number

if n%2==1,  n is an odd number

 

Calculate the sum of odd and even numbers using for loop

Program 1

This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from the from 1 to entered digits using a for loop.

#Python program to calculate sum of odd and even numbers using for loop
max=int(input("please enter the maximum value: "))
even_Sum=0
odd_Sum=0

for num in range(1,max+1):
    if (num%2==0):
        even_Sum=even_Sum+num
    else:
        odd_Sum=odd_Sum+num

print("The sum of Even numbers 1 to {0} = {1}".format(num,even_Sum))
print("The sum of odd numbers 1 to {0} = {1}".format(num,odd_Sum))

case 1

please enter the maximum value: 10
The sum of Even numbers 1 to 10 = 30
The sum of odd numbers 1 to 10 = 25

case 2

please enter the maximum value: 100
The sum of Even numbers 1 to 100 = 2550
The sum of odd numbers 1 to 100 = 2500

 

Calculate the sum of odd and even numbers using while loop

Program 2

This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from the from 1 to entered digits using a while loop.

#Python program to calculate sum of odd and even numbers using while loop
max=int(input("please enter the maximum value: "))
even_Sum=0
odd_Sum=0

num=1
while (num<=max):
    if (num%2==0):
        even_Sum=even_Sum+num
    else:
        odd_Sum=odd_Sum+num

    num+=1

print("The sum of Even numbers 1 to entered number", even_Sum))
 print("The sum of Even numbers 1 to entered number", odd_Sum))

 

case 1

please enter the maximum value: 20
The sum of Even numbers 1 to Entered number = 110
The sum of odd numbers 1 to Entered number = 100

Suggested for you

for loop in Python

while loop in Python

if statements in Python

 

Similar post

Python program to check whether a number odd or even

Python program to check a number odd or even using function

Python program to display even and odd number in the given range

Python code to display all even and odd numbers from 1 to n

Python code to display all even and odd numbers without if

 

 

 

C program to calculate sum of odd and even numbers
Separate odd and even number in a list to different two list
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • Sir .. Apka Koi Youtube Account Hai To Please Btaiyye jisme aapne Python ke codes ko smjhaa rakha ho....Aapke codes bahut achhe hai .....Ya fir aapka koi Google par page ho jisme python explain ho .... so please inform me...
    My Email id is agarwalashutosh84@gmail.com

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago