Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Write a program to list all even numbers between two numbers

 
 

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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

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:15
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