Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Java
// prints even numbers between 0 and 100

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

    if((i%2) != 0) continue; // iterate

    System.out.println(i);
    }
   }
 }
Posted
Updated 18-Aug-15 1:37am
v2

1 solution

It simply skips odd numbers.
Since the % operator gives the reminder of the integer division, for every even number ev, (ev % 2) result is 0. On the other hand, for every odd number od, (od % 2) result is 1.

You know, a more efficient algoritm would be
Java
public static void main(String args[]) 
{
  int i;

  for(i = 0; i<=100; i+=2) 
  {
    System.out.println(i);
  }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Aug-15 10:18am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900