+ 2

How to output only even numbers? Given that x = 0 while x<=10: print(x) x+=1

Please help me to output only even numbers

28th Sep 2023, 4:06 PM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
15 ответов
+ 4
x=2 while x<10: x=x+2 print(x) This is the easiest way out of all.
29th Sep 2023, 7:05 AM
Shivam Pandey
Shivam Pandey - avatar
+ 4
your indentation is wrong. your code runs from x = 0 up to x = 10, then the while loop is finish. after that comes the if part, with x = 11 and thatsway you don‘t get the even nums. thats the rigth way. the if condition is part of even while loopand prints the even nums. x = 0 while x<=10: #print(x) x+=1 if (x%2) == 0: print(x)
28th Sep 2023, 5:20 PM
Angela
Angela - avatar
+ 3
You can use the remainder (%) if(x%2==0) { // print x }
28th Sep 2023, 4:28 PM
JavaBobbo
JavaBobbo - avatar
+ 3
x = 0 while x<=10: if (x % 2) == 0: print(x) x+=1
28th Sep 2023, 5:23 PM
JavaBobbo
JavaBobbo - avatar
+ 2
It's while loop lesson code. Have you done it??
28th Sep 2023, 4:55 PM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
+ 1
Then you are doing something wrong, it will only print even numbers. Ofc i know the while loop is the same for all languages.. Post your code
28th Sep 2023, 4:58 PM
JavaBobbo
JavaBobbo - avatar
+ 1
Wait I'll do
28th Sep 2023, 4:59 PM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
+ 1
x = 0 while x<=10: print(x) x+=1 if (x%2) == 0: print(x)
28th Sep 2023, 5:01 PM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
0
It is in python??
28th Sep 2023, 4:34 PM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
0
Almost the same.. use this if (x%2) == 0:
28th Sep 2023, 4:43 PM
JavaBobbo
JavaBobbo - avatar
0
No it's output is 1 2 3 4 5 6 7 8 9 10
28th Sep 2023, 4:53 PM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
0
I don't know how to output only even numbers this code output all numbers 1 to 10
28th Sep 2023, 4:56 PM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
0
Thanks for your answers I have done it.
29th Sep 2023, 2:28 AM
Shiva Vishwakarma
Shiva Vishwakarma - avatar
0
print(*[i for i in range(2,11,2)])
29th Sep 2023, 2:43 PM
Sunil
Sunil - avatar
0
Java Int x = 0; X<=10; X++; If (x%2==0){ System.out.println("x is even") }
29th Sep 2023, 8:45 PM
Davy Mang'o
Davy Mang'o - avatar