separate Even and Odd numbers

java program to separate and print odd digits followed by even digits in given int value.

for example input int value is: 568432196 and output should be : 531968426

package com.il.basics.programs;

public class OddEvenSeperation {
	public static void main(String[] args) {
		int inputInt = 568432196;
		String inputStr = String.valueOf(inputInt);
		char inputstrArr[] = inputStr.toCharArray();
		StringBuilder sbEven = new StringBuilder();
		StringBuilder sbOdd = new StringBuilder();
		for (char c : inputstrArr) {
			int num = Integer.valueOf(String.valueOf(c));
			if (num % 2 == 0) {
				sbEven.append(num);
			} else {
				sbOdd.append(num);
			}
		}
		System.out.println("Ouput Number is :" + sbOdd.toString()
				+ sbEven.toString());
	}
}
****output****
Ouput Number is :531968426

Leave a Reply

Your email address will not be published. Required fields are marked *