answersLogoWhite

0

final int limit = 1000000;

// Print evens...

for(int i = 0; i <= limit; i += 2) {

System.out.print(i + " ");

}

// Print odds...

for(int i = 1; i <= limit; i += 2) {

System.out.print(i + " ");

}

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
More answers

public class Test {

public static void main(String[] args){

for(int i = 0; i<100; i++){

if(i%2 == 0){

System.out.println(i);

}

}

}

}

OR

public class Test {

public static void main(String[] args){

for(int i = 2; i<100;i+=2){

System.out.println(i);

}

}

}

User Avatar

Wiki User

14y ago
User Avatar

public class forloop {

public static void main(String args[])

{

for(int i=50;i<=80;i++)

{

if(i%2==0)

{

System.out.println(i);

}

}

}

}

User Avatar

Wiki User

11y ago
User Avatar

// finds this sum in range (1, 100]

int sum = 0;

for(int i = 2; i < 101; i+=2) {

System.out.println("Adding : " + i);

sum += i;

}

System.out.println("Sum : " + sum);

User Avatar

Wiki User

10y ago
User Avatar

Use a for loop, and increment two at a time. Something like this:

sum = 0;

for (i = 1, i <= 100, i += 2)

sum += i;

Explanation: "i" starts at 1, the loop continues running (and adding to the sum) while i is less than or equal to 100, and i is incremented two at a time.

User Avatar

Wiki User

12y ago
User Avatar

Add your answer:

Earn +20 pts
Q: To print even numbers in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp