Basic Java Examples

Even Odd Number Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

21 Comments

  • //An easier way to compile by ForEach concept.
    public class FindEvenOrOddNumber
    {
    public static void main(String[] args)
    {
    int nums[]={1,2,3,4,5,6,7,8,9,10};

    for(int x:nums) //Using ForEach concept
    {
    if(x%2 == 0)
    System.out.println(x + ” is even number.”);
    else
    System.out.println(x + ” is odd number.”);
    }
    }
    }

  • System.out.println(numbers[i] + ” is even number.”);
    OR

    System.out.println(x + ” is even number.”);

    Thanks for the help but .why they using + sign after x or after numbers[i]+ ??..thanks

    • The ‘+’ in the “System.out.println” code is there to add the strings together. This way you can write down a variable and the value of it will appear rather than the actual name of the variable.

  • more easier way
    ____________________________________________________
    class test
    {
    public static void main(String args[])
    {
    int n=100,r;
    r=n%2;
    if(r==0)
    system.out.print(“given no is even”);
    else
    system.out.print(“given no is odd”);
    }
    }

    • These codes are just the basics for those who are new to Java or are experiencing code difficulties. They help you understand the basics so when you get to the harder stuff, it will be somewhat broken down for you. In short: Makes the programmer’s life much more simpler.

  • Rather simple piece of code. This should be easy for you before you try to make a code to find out if a number is a prime or composite.

  • in the array is replaced by -1 if it is an odd number, with -2 if it is greater than 50, and with -3 if it is an odd number and greater than 50

  • public class OddEven {

    public static void main(String []args) {

    for (int a=1; a<=1000; a=a+1)

    //int b = 0;

    if (a%2==0)

    System.out.println(a+” is even”);

    else

    System.out.println(a+” is odd”);

    }

    }:D

  • Hi Friends ,

    Please any one help to sort the below array from even to odd .

    I have a array like

    int a[] ={200,20,29,31,50,51} ;

    The requirement is we have to sort the array like

    int []={20,50,200,29,31,51};

    • public class OrderingAscArray {

      public static void main(String[] args) {
      int a[] ={200,20,29,31,50,51} ;
      int[] b=new int[6];
      int i;
      ArrayList<integer> c=new ArrayList<integer>();
      ArrayList<integer> d=new ArrayList<integer>();
      for(i=0;i<6;i++){
      if(a[i]%2==0)
      c.add(a[i]);
      else
      d.add(a[i]);
      }
      Collections.sort(c);
      Collections.sort(d);
      c.addAll(d);
      System.out.println(c);
      }
      }

    • First divide even or odd numbers using int a%2==0.. than this two are even and odd numbers are store in other variable. than use a sorting array.. shift operation

  • /*
    Even Odd Number Example
    This Java Even Odd Number Example shows how to check if the given
    number is even or odd.
    */

    public class FindEvenOrOddNumber {

    public static void main(String[] args) {

    //create an array of 10 numbers
    int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};

    for(int i=0; i < numbers.length; i++){

    /*
    * use modulus operator to check if the number is even or odd.
    * If we divide any number by 2 and reminder is 0 then the number is
    * even, otherwise it is odd.
    */

    if(numbers[i]%2 == 0)
    System.out.println(numbers[i] + ” is even number.”);
    else
    System.out.println(numbers[i] + ” is odd number.”);

    }

    }
    }

    /*
    Output of the program would be
    1 is odd number.
    2 is even number.
    3 is odd number.
    4 is even number.
    5 is odd number.
    6 is even number.
    7 is odd number.
    8 is even number.
    9 is odd number.
    10 is even number.
    */

  • import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    public class Even_Odd_Number_01 {

    public static void main(String[] args)throws IOException {

    // TODO Auto-generated method stub

    int numbers[]={78,52,36,22,11,15,19,117,113};

    int i;

    String z;

    try {

    //numbers[0]=78 ;

    //numbers[1]=52 ;

    //numbers[2]=36 ;

    //numbers[3]=22 ;

    //numbers[4]=11 ;

    //numbers[5]=15 ;

    //numbers[6]=19 ;

    //numbers[7]=117 ;

    //numbers[8]= 113 ;

    BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));

    System.out.print(“Enter the array: “);

    z=BR.readLine();

    i=Integer.parseInt(z);

    //System.out.print(numbers[i]);

    //for(i=0;i<numbers.length;i++) if=”” (numbers[i]%2=”=0)” system.out.print(“this=”” array=”” is=”” odd=”” array=”” “=”” );=”” else=”” system.out.print(“this=”” array=”” is=”” even=”” “);=”” }=”” catch=”” (exception=”” e)=”” {=”” system.out.print(“wrong=”” input=”” man!!!=”” “=”” );=”” todo:=”” handle=”” exception=”” }=”” }=”” }=””>

  • package javaapplication1;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    /**
    *
    * @author Ajoon
    */
    public class oddeven {
    void oddevn(){
    int []arry=new int[20];
    try{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println(“Enter the value into array”);
    for(int i=0;i<arry.length;i++) {=”” arry[i]=”Integer.parseInt(br.readLine());” }=”” }=”” catch(ioexception=”” ioe){=”” system.out.println(io=”” error”+ioe);=”” }=”” for(int=”” i=”0;i&lt;arry.length;i++){” int=”” a=”arry[i]%2;” if(a=”=0)” {=”” system.out.println(“even=”” numbers”+arry[i]);=”” }=”” else=”” {=”” system.out.println(“odd=”” numbers”+arry[i]);=”” }=”” }=”” }=”” }=””>

Sponsors

Facebook Fans