Write a Python Program to Calculate Sum of Odd Numbers from 1 to N using While Loop, and For Loop with example.

**TIP: **I suggest you to refer Odd numbers from 1 to N article to understand the logic behind printing Odd numbers.

## Python Program to Calculate Sum of Odd Numbers from 1 to N using For Loop

This Python program allows user to enter the maximum value. Next, it is going to calculate the sum of odd numbers from 1 to user entered maximum value.

In this example, For Loop is used to keep the odd numbers are between 1 and maximum value.

# 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**

## Python Program to display Sum of Odd Numbers from 1 to N without If

This program is same as above, but we used third parameter inside the for loop to eliminate If block.

# 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, 2): print("{0}".format(number)) Oddtotal = Oddtotal + number print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, Oddtotal))

**OUTPUT**

## Python Program to find Sum of Odd Numbers using While Loop

In this program, we replaced For Loop with While Loop.

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

**OUTPUT**

## Python Program to find Sum of Odd Numbers from 1 to 100

This example allows user to enter Minimum and maximum value. Next, it will calculate sum of odd numbers from Minimum to maximum value.

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

**OUTPUT**