Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

Java Program to print Even numbers till N

Get input n and print n even numbers.

Sample Input 1:

7

Sample Output 1:

2 4 6 8 10 12 14

Program or Solution

					    
							
					    
					

Program Explanation

Instruction(s) inside the for block{} are executed repeatedly till the second expression (j<=lim) is true.

Here i and j are initialized to 2 and i is incremented by 1 and j is incremented by 1.

for each iteration, instructions inside the for block are executed unless j becomes greater than n.

so value of i (2,4,6,8,.....) (n odd numbers) in each iteration will be printed using system.out.println.