BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to print Even numbers from 1 to n or 1 to 100

Last Updated: April 16, 2019 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a Java program to display even numbers from 1 to n which means if the value of n is 100 then this program will display the even values between 1 to 100.

Program to display even numbers from 1 to n where n is 100

In the following example we are displaying the even numbers from 1 to n, the value of n we have set here is 100 so basically this program will print the even numbers between 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. To understand this program you should have the basic knowledge of for loop in Java and if statement.

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

Related Java Examples

1. Java Program to print odd numbers from 1 to 100
2. Java program to check even or odd number
3. Java program to check if a given number is perfect square
4. Java Program to find GCD of two numbers

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Sphenic Number in Java – Check and Print all numbers in a range
  3. Java Program for Selection Sorting
  4. Java Program to display first n or first 100 prime numbers
  5. Java program to check prime number

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap