Home Java Beginners Write a program to list all even numbers between two numbers
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Write a program to list all even numbers between two numbers

Advertisement
Here you will learn to write a program for listing out all the even numbers between two numbers.

Write a program to list all even numbers between two numbers

     

Java Even Numbers - Even Numbers Example in Java:
Here you will learn to write a program for listing out all the even numbers between two numbers. For this first create a class named AllEvenNum under the java.io package. Now use the try/catch exception to avoid any kind of input error. After this create a buffer class in which all the input data are stored and modified. Then give message as to "Enter number" in the System method. 

As we have to find out all the even numbers between 1 and the input number, define an integer variable 'num'. Now apply ParseInt method that parses the string character into decimal integer. Again apply for loop in which define an integer i=1 and i<=  num also with an increment operator. Then apply the if condition that i/2=0 i.e. to find even numbers which are divided by the integer 2. In the end apply the catch exception. 

Now and compile and run the program, and enter your desired number to get all even numbers between 1 and this numbers. 

Here is the code of the program:

import java.io.*;

class AllEvenNum{
  public static void main(String[] args) {
  try{
  BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter number : ");
  int num = Integer.parseInt(br1.readLine());
  System.out.println("Even Numbers:");
  for (int i=1;i <=num ; i++){
  if(i%2==){
  System.out.print(i+",");
  }
  }
  }
  catch(Exception e){}
  
  }
}

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 2, 2007

Ask Questions?    Discuss: Write a program to list all even numbers between two numbers   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Sheng
July 7, 2011
ask a question

...hello I'm a student and as a project. he ask us to make a mailbox of 1-200. and with this mailboxes the 1st task is to open all the even numbers.then the 2nd task is open the no.3 mailbox followed by next 3rd mailbox and so on. but the the problem is how can i open a closed mailbox and closed the open one...Hope to help me
core java t
June 23, 2011
core java tutorial

core java tutorial
Nick Fifis
June 30, 2011
Errors

I followed the tut to the letter, but I keep getting errors when I try to run it. "The left-hand side of an assignment must be a variable Syntax error, insert ")" to complete Expression Syntax error, insert "AssignmentOperator Expression" to complete Expression Syntax error, insert "Finally" to complete BlockStatements at myProject.main(myProject.java:17)" I'm using eclipse because a friend of mine recommended it though I doubt that has anything to do with the problems.
Sheryl Lubo
September 21, 2011
Object Oriented Programming

please help me in this subject...
Sheryl Lubo
September 21, 2011
Object-Oriented Programming

please help me in my Program today....
Emmanuel
October 2, 2011
java

Try explain for us what try and catch exception meaan in java
phoebe mae caballero
January 10, 2012
java

i love this java topic
vaibhav parikh
January 29, 2012
correction

please correct above i/2=0,in theory description it should be i%2=0 as wriiten in program...thank u.
darwen
March 6, 2012
computer science

i want to know and i want to learn the cisco and how to ip add thats all thank you
matthew
May 2, 2012
error in code

error line 6 ';' needed at the end of line.
ycliff
June 29, 2012
java problems

Does "import java.util.Scanner;" do the same work as "import java.io.*;" or why did the programmer below choose the latter ? import java.io.*; class AllEvenNum{ public static void main(String[] args) { try{ BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number : "); int num = Integer.parseInt(br1.readLine()); System.out.println("Even Numbers:"); for (int i=1;i <=num ; i++){ if(i%2==0 ){ System.out.print(i+","); } } } catch(Exception e){} } } //thanks for yo help
Norfaisa
September 26, 2012
hhhh

programming is so very hard..promise
Rajkumar M
January 6, 2013
Java programs

Basic of a programs is a all people
Rajkumar M
January 6, 2013
Basic programs

Java programs all peoples reachied
HARISH K R
March 6, 2013
WRITE A PROGRAM TO PRINT 8000TH PRIME NUMBER

HI FRIENDS I NEED A SOLUTION FOR FINDING 8000TH PRIME NUMBER
DMCA.com