Java Program to Print Even Numbers from 1 to N - The Coding Shala

Home >> Java Programs >> Java Program to Print Even Numbers from 1 to N

 In this post, we will learn how to Display Even Numbers from 1 to n using Java Program.

Java Program to Print Even Numbers from 1 to N

Write a Java Program to display even numbers from 1 to given n. We can use Java loops here.

Example 1:
Input: 5
Output: 2


We can use a for loop from 1 to n and will check if the current number is divisible by 2, if it is divisible by 2 then it's an even number else not.

We can also start from 2 and increase the current number in the loop by 2 and print all the numbers till n.

Java Program: 


import java.util.Scanner;

public class Main {

    public static void printEvenNumbers(int n) {
        for(int i = 1; i <= n; i++) {
            // if number is divisible by 2 then its even
            if(i%2 == 0) {

    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        System.out.println("Enter value of n");
        int n = sc.nextInt();
        System.out.println("Even numbers from 1 to " + n + " are: ");


Enter value of n
Even numbers from 1 to 17 are: 

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.


Popular Posts from this Blog

LeetCode - Crawler Log Folder Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

First Unique Character in a String Java - The Coding Shala

Add two numbers in Scala - The Coding Shala