FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Print Even numbers from 1 to n or 1 to 100


If an integer number(never a fraction number) is exactly divisible by 2 which means it yields no remainder when divided by 2 then it is an even number. This same logic we are using here to find the even numbers. We are looping through 1 to n and checking each value whether it is evenly divisible by 2 or not, if it is then we are displaying it.

class JavaExample {
   public static void main(String args[]) {
	int n = 100;
	System.out.print("Even Numbers from 1 to "+n+" are: ");
	for (int i = 1; i <= n; i++) {
	   //if number%2 == 0 it means its an even number
	   if (i % 2 == 0) {
		System.out.print(i + " ");
	   }
	}
   }
}

Output

Even Numbers from 1 to 100 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 
30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 
78 80 82 84 86 88 90 92 94 96 98 100

You may also Find this interesting

Program to find all the prime numbers

Program to Display Area of a Rectangle

Program to Display Area of a Triangle

Program to Display Area of a Circle

Program to Display Vowels and Constant counts

Program to Display reverse of a string

Program to Display reverse of a string using recursion

Program to Convert Character to String

Program to sort Strings