+ 5

While loops

Hello, I'm stuck at this problem. Can somebody help? You are given a program that outputs all the numbers from 0 to 10. Change the code to make it output only the even numbers. We should use while... x = 0 while print(x) x += 1 That's what I wrote but it doesn't work x = 0 while x % 2 == 0 : print(x) x += 1

17th Apr 2021, 7:34 AM
Simone Bucci
Simone Bucci - avatar
2 Answers
+ 3
x = 0; while x <= 10: x+= 1; #Even numbers if x % 2 == 0: print(x) I think you forget to use if and else ,to perform a conditional task.
17th Apr 2021, 7:40 AM
Tejas [Inactive]
Tejas [Inactive] - avatar
0
If you use while x % 2 == 0, the loop will end when the value of x is 1. https://code.sololearn.com/cZx1YMs86Hbc/?ref=app
17th Apr 2021, 7:43 AM
Soumik
Soumik - avatar