Twist alternate numbers in an Integer in java

Recently I have been asked about twist numbers in an integer.

Example : 123456 >> 214365

import java.util.Scanner;

public class TwistNumbers {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
 int input = scan.nextInt();
 System.out.println("Given input is " + input);
 int finalresult = 0;
 int incrementer = 1;
 while (input > 0) {
 int dividend = input % 100;
 int result = 0;
 while (dividend > 0) {
 int remainder = dividend % 10;
 result = result * 10 + remainder;
 dividend = dividend / 10;
 }
 input = input / 100;
 finalresult = result * incrementer + finalresult;
 incrementer *= 100;

}
System.out.println("Ouput is " + finalresult);
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s