Yes I am new to learning java. I am just learning loops. For my online college class I have to write a program using the for loops. This program needs to give all even numbers between 0-100. I have a program written that gives me all numbers 1-100. Can anyone help me solve where need to do to make it give me even numbers? Here is what I have so far.
public class for2{
public static void main(String[] args){
for(int x = 2; x < 101; x++)
{
System.out.println("x =" + x);
}
}
}

Like I say it gives me all numbers when I need even numbers. So any help would be nice and thank you.

This '%' gives the remainder of a number. An even number is the one that can devided by 2 so the remainder of 2 should be 0:

if (number%2==0) {
   System.out.println("Number: "+number + " is even.");
}

Have fun my friend:

public class for2{
  public static void main(String[] args){
    for(int x = 2; x < 101; x++)
    {
     if(x%2==0)
      System.out.println("x =" + x);
    }
  }
}

Cheers

____________________________
javaAddict is correct. :)

Edited 6 Years Ago by mytime19: Someone posted the stuff at the same time :)

This question has already been answered. Start a new discussion instead.