0
Sum of even number in python using while loop
Given a number N, print sum of all even numbers from 1 to N.
20 Réponses
+ 4
Ok, i think you want to take a number like 234567 and sum the even digits 2+4+6 = 12?
Please do a try by yourself first. If you get stuck you can put your code in playground and link it here. Thanks!
+ 4
num ,sum=2,0
while numislessthanN:
add num to the sum
increase num by 2
Printthesum
+ 3
a = int(input())
sum = 0
for i in range(a +1):
if i%2 == 0:
sum += i
print(sum)
hope it helped. You take a look at our community guidelines. Actually your question is an inappropriate one.
https://www.sololearn.com/discuss/1316935/?ref=app
+ 3
print((2+N-N%2)*N/4)
+ 3
Muhammad, may be there is a misunderstanding. May be you can read the question again and then check the code?
+ 2
What can you already do yourself?
+ 2
Muhammad, if i input 1234 i get the result of 1234.
+ 1
Satyam Patel Can you explain your code? I don't see how it would work or your reasoning that it would in the first place.
+ 1
It can also be done in a list comprehension:
n = 10
print(sum([i for i in range(1,n+1) if i % 2 == 0]))
# output is 30
+ 1
Satyam Patel, i think we need some clear information what the task is.
(1) Should we sum the even digits in a number. like number is 1351204, digit ,0 and 4 are even and give sum of 6 ?
(2) or should we sum digits at even (index) position of 1351204, which is 1, 5, 2, 4 which gives a sum of 12 ?
(3) or are you talking of a range of numbers from e.g. 1 to 60, where all even numbers have to be summarized ?
Thanks for your support.
+ 1
n=int(input())
sum=0
i=1
while(i<=n):
if(i%2==0):
sum+=I
print(sum)
0
Using while loop
0
n=int(input ())
sum=0
while n%2==0:
sum=sum+n
n=n-1
print(sum)
0
Hello everybody!
Here is my code using while loop
a=int(input())
b=0
c=0
while b<=a:
c+=b
b+=2
print(c)
0
Made i a mistake,
But the code is working. Are you tried the code?
0
Lothar thanks, i understand the ques.
I thought that i should find sum of even numbers in range()
I think you understood this too
0
Muhammad Satyam thought and did it like me
0
num = int(input())
R = list(range(0,num,2))
print(sum(R))
0
write a Python program to input a number and print the sum of the all even number from one two num use for or while loop
0
write a Python program to input a number and print the sum of the all even number from one two num use for or while loop