Wednesday, November 3, 2010

1234 -> 4123 -> 3412number pattern

/*

Write a program that generated output something like this (here 1234 is input).
Here, keep all the numbers as integers only. Do not convert them to string or character:
1234
2341
3412
4123

note: here 0 will not be inputted at ay place
*/


import java.util.*;

public class
a
{


static
int length(int n)
{


int
len = 0;
while
(n > 0)
{


len++;
n /= 10;
}

return
len;
}


static
int generate(int n)
{

int
temp = (int) Math.pow(10, length(n) - 1);

int
temp2 = n / (int) Math.pow(10, length(n) - 1);

temp = n % temp;
temp = temp * 10;

temp = temp + temp2;
return
temp;
}


public static
void main(String a[])
{

int
n;

Scanner s = new Scanner(System.in);
n = s.nextInt();

int
temp = n;
/* n-1 if you do not want to include the original digit and
n if you want to include original digit
*/

for
(int i = 0; i < length(n) - 1; i++)
{


temp = generate(temp);
System.out.println(temp);
}

}
}

No comments:

Post a Comment