+ 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

15 Answers

+ 4

x=2
while x<10:
x=x+2
print(x)
This is the easiest way out of all.

+ 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)

+ 3

You can use the remainder (%)
if(x%2==0) {
// print x
}

+ 3

x = 0
while x<=10:
if (x % 2) == 0:
print(x)
x+=1

+ 2

It's while loop lesson code. Have you done it??

+ 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

+ 1

Wait I'll do

+ 1

x = 0
while x<=10:
print(x)
x+=1
if (x%2) == 0:
print(x)

0

It is in python??

0

Almost the same.. use this
if (x%2) == 0:

0

No it's output is
1
2
3
4
5
6
7
8
9
10

0

I don't know how to output only even numbers this code output all numbers 1 to 10

0

Thanks for your answers I have done it.

0

print(*[i for i in range(2,11,2)])

0

Java
Int x = 0;
X<=10;
X++;
If (x%2==0){
System.out.println("x is even")
}