Sum of even number in python using while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Sum of even number in python using while loop

Given a number N, print sum of all even numbers from 1 to N.

19th May 2020, 5:32 PM
Satyam Patel
Satyam Patel - avatar
20 Answers
+ 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!
19th May 2020, 5:38 PM
Lothar
Lothar - avatar
+ 4
num ,sum=2,0 while numislessthanN: add num to the sum increase num by 2 Printthesum
19th May 2020, 6:23 PM
Oma Falk
Oma Falk - avatar
+ 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
19th May 2020, 5:35 PM
M Tamim
M Tamim - avatar
+ 3
print((2+N-N%2)*N/4)
19th May 2020, 5:43 PM
Oma Falk
Oma Falk - avatar
+ 3
Muhammad, may be there is a misunderstanding. May be you can read the question again and then check the code?
21st May 2020, 11:41 AM
Lothar
Lothar - avatar
+ 2
What can you already do yourself?
19th May 2020, 5:36 PM
Oma Falk
Oma Falk - avatar
+ 2
Muhammad, if i input 1234 i get the result of 1234.
21st May 2020, 12:02 PM
Lothar
Lothar - avatar
+ 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.
19th May 2020, 5:49 PM
Nboumakis
+ 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
20th May 2020, 5:14 PM
Lothar
Lothar - avatar
+ 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.
21st May 2020, 12:26 PM
Lothar
Lothar - avatar
+ 1
n=int(input()) sum=0 i=1 while(i<=n): if(i%2==0): sum+=I print(sum)
21st May 2020, 5:37 PM
KEDAR U SHET
KEDAR U SHET - avatar
0
Using while loop
19th May 2020, 5:38 PM
Satyam Patel
Satyam Patel - avatar
0
n=int(input ()) sum=0 while n%2==0: sum=sum+n n=n-1 print(sum)
19th May 2020, 5:40 PM
Satyam Patel
Satyam Patel - avatar
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)
21st May 2020, 10:56 AM
Muhammadamin
Muhammadamin - avatar
0
Made i a mistake, But the code is working. Are you tried the code?
21st May 2020, 11:48 AM
Muhammadamin
Muhammadamin - avatar
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
21st May 2020, 1:29 PM
Muhammadamin
Muhammadamin - avatar
0
Muhammad Satyam thought and did it like me
21st May 2020, 1:31 PM
Muhammadamin
Muhammadamin - avatar
0
num = int(input()) R = list(range(0,num,2)) print(sum(R))
21st May 2020, 3:46 PM
salman haider
salman haider - avatar
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
13th Mar 2023, 3:01 AM
Questions paper
Questions paper - avatar
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
13th Mar 2023, 3:01 AM
Questions paper
Questions paper - avatar