Saturday, 23 July 2016

Even Odd In Java

Get comma separated String of numbers from user and print the set of odd numbers and even numbers.
sample input:
1,2,3,4,5,6,7,8,9,10
Sample output:
Odd Numbers:
1,3,5,7,9
Even Numbers:
2,4,6,8,10
Sample input:
20,30,40
Sample output:
Even Numbers:
20,30,40


/*Write your code here */
import java.util.*;
class mno{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        String a=s.nextLine();
        String[] b=a.split(",");
        
        int[] ar=new int[b.length];
        String e="",o="";
        
        for(int i=0;i<b.length;i++){
            ar[i]=Integer.parseInt(b[i]);
        }
        
        for(int i=0;i<ar.length;i++){
        if(ar[i]%2==0){
            e=e+ar[i]+",";
        }else
             o=o+ar[i]+",";

        }
        if(o.length()!=0){
            o=o.substring(0,o.length()-1);
            System.out.println("Odd Numbers:");
            System.out.println(o);
        }
        if(e.length()!=0){
            e=e.substring(0,e.length()-1);
            System.out.println("Even Numbers:");
            System.out.println(e);
        }
        
    
    }
}

No comments: