find sum of even number s upto n in python
Answers
Answered by
5
Answer:
First, we declare one variable ” sum ” with value 0, and then we are going to use this variable to store sum of all even numbers between 1 to N. Now after taking input (N) from user, we have to check if the current variable “i” is even or not inside the loop . If it is even we have to add it to variable ” sum ” otherwise continue with the loop.
Once the loop is completed we have to print the variable ” sum “ containing the sum of all even numbers up to N.
Program
# Take input from user.
num = int(input("Print sum of even numbers till : "))
total = 0
for i in range(1, num + 1):
# Check for even or not.
if((i % 2) == 0):
total = total + i
print("\nSum of even numbers from 1 to"
Similar questions
India Languages,
2 months ago
Science,
5 months ago
Physics,
5 months ago
Math,
11 months ago
Math,
11 months ago