Find the sum of odd numbers between 1 to n

In this program, we will find the sum of odd numbers between 1 to n.

Steps to solve the program
  1. Use for loop with range function to iterate over numbers between 1-n.
  2. Create a variable total and assign its value equal to 0.
  3. During the iteration check, if the number is odd and add only those numbers to the total variable.
  4. Print the output.
				
					n= int(input("Enter the last number: "))
total = 0

for i in range(1,n+1):
    if i%2 != 0:
        total += i
    
print(total)
				
			

Output :

				
					25
				
			

find the sum of all even numbers between 1 to n

count the number of digits in a number

Leave a Comment