Write a Python program to find the sum of all odd numbers between 1 and a given number.
n=int(input("enter the number"))
sum=0
for i in range(0,n+1):
if i%2!=0:
sum+=i
print(sum)
Output :
enter the number10
25
The input() function is used to get input from the user, and int() is used to convert the input to an integer.
Initializes a variable called sum to 0. This variable will be used to keep track of the sum of odd numbers.
The loop variable is i, which takes on the values from 0 to n in each iteration.
Checks if the current value of i is not divisible by 2, i.e., if it is an odd number.
If i is an odd number, it is added to the current value of the sum.
After the loop completes, the final value of the sum is printed.
If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).
HAPPY LEARNING !!!