Sunday, 4 January 2015

Sort Odd and Even Number in Java

In this section, I post about java programming language. In previous campus I learnt C# language, then I moved to another campus, definitely, I have to change my language because different course syllabus. C'est rien pour moi! new campus = new life = new language = new destination. Voila, c'est la vie! must go on!

I've written the java code when lecturer gave us the assignment about how to sort odd and even number. Here, I'll show you how to solve direct sort and using array that can store certain number of data.



  • Use Array


package arraylist;

import java.util.*;
public class Arraylist{
    public static int[] array = new int[0]; //Creating and object array
    
    //Creating list integer array even and odd
    public static List even = new ArrayList<>();
    public static List odd = new ArrayList<>();

    public static void classify(){
        
        //Deciding method either the value is odd or even
        for(int i = 0 ; i < array.length ; i++){
            if(array[i] % 2 == 0)
                even.add(array[i]);
            else
                odd.add(array[i]);
        }
    }//end classify method

    public static void display(List list){
        
        //Display the value inside the list integer
        for(Integer i : list)
            System.out.print(i+" ");
    }

    public static void main(String[] args){
        //Declaring variable and creating scanner
        int num,count=1;
        Scanner in = new Scanner(System.in);
        
        //Asking user how many number to input
        System.out.print("how many input? ");
        count = in.nextInt();
        array = new int[count]; //Assigning the total of index in array
       
           for(int i = 0 ; i < array.length ; i++)
           {
            System.out.print("number would like to enter ");
            num = in.nextInt();
            array[i]=num; //Assigning to the array according to the index
            }
    
        classify(); //Call method to classify
        
        System.out.print("Even number is ");
        display(even); //Call method to display even
        System.out.print("\nOdd number is ");
        display(odd); //Call method to display odd
        System.out.print("\n");
    }
}//end Class



  • Use Direct method
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package oddevennumber;

import java.util.Scanner;

public class OddEvenNumber {
    public static void main(String[] args)throws Exception {
        
        int num = 0;

        Scanner input = new Scanner(System.in);

        while(num!=-1)
        {
            System.out.println("\n###############################");
            System.out.println("Press '-1' to exit this Even/Odd checker");
            System.out.println("###############################");
            System.out.print("number would like to enter :");
            num = input.nextInt();
            AllDigitsOod(num);
        }
        System.out.println("Thanks!");
    }
    
    public static int AllDigitsOod(int x)
    {
        boolean a=(x%2==0);
        if (a ==true)
            {
                 System.out.println(x+" is even\n");
            }          
        else if(x==-1)
        {
            System.out.println(" You exit from this game");
                }
            else 
            {
                System.out.println(x+" is odd\n");
            }
        
        return  x;
    }
}
Share:

1 comment:

  1. A Terrific Relocation Guide For Everyone

    packers and movers pune
    Welcome to here, a premium online tool helping consumers relocate from one place to another with household belongings or commercial commodities. We have made it easy to find right services, right moving companies and other related information in different major cities and towns of India packers and movers in pune.
    movers and packers in pune

    ReplyDelete