+ 1

class MyClass { public static void main(String[ ] args) { int a=0; int even = 0; for(even = 0;even <= 100;++even

} System.out.println(even*2); why the output exceeding 100 in above code

2nd Mar 2017, 2:54 PM
Sahil
Sahil - avatar
2 Réponses
+ 1
When even <= 100, even *2 <= 200. So 200 is printed. If you want to print even numbers from 0 to 100, you should use for (even = 0; even *2 <= 100; ++even) { or for (even = 0; even <= 100; even += 2) {
2nd Mar 2017, 3:21 PM
Twelfty
Twelfty - avatar
+ 1
I can't really understand your question, but I am assuming you want to print even numbers from 0 to 200, in that case you should put 'System.out.println(even*2);' inside the for loop, sth like this: class MyClass { public static void main(String[ ] args) { for(int even = 0;even <= 100;++even) { System.out.println(even*2); } } } But if you want to print even numbers from 0 to 100, then just use the loops provided by Twelfty and System.out.println(even); instead of even*2.
2nd Mar 2017, 4:14 PM
Lindon Nuha
Lindon Nuha - avatar