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
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
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
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
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…
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