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. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.