-4

For example:

If the input is 550 the answer is 0
If the input is 32847923846 the answer is 2

The length of the input is variable.

I have tried arraylist and buffered reader class but i am not being able to get because the input's length is variable

Navin
  • 3,681
  • 3
  • 28
  • 52
ak47_kalani
  • 15
  • 1
  • 8

3 Answers3

2

Step one, round the number down to the nearest 10, as seen here.

int round(double i, int v){
    return Math.floor(i/v) * v;
}

And then minus this number from the original.

int value = 34554674;
int roundedDown = round(value, 10);

int smallest = value - roundedDown;

As seen by this Ideone.

Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89
  • If I understand OP correctly he looks for the smallest digit. Take a look at his `32847923846` example, the expected output is `2` and not `6`. The expected output for `123` would be `1`. But he accepted this answer, so whatever. – Sascha Wolf Jun 02 '15 at 13:57
  • 1
    Ah yeah.. I must have misread, but yeah you're correct. Either he/she will comment and ask for a correction or this is one of those times where two wrongs made a right. – christopher Jun 02 '15 at 14:19
  • Or he/she will submit a wrong solution for his/her homework. I would be fine with that. – Sascha Wolf Jun 02 '15 at 14:40
  • 2
    I feel like some sort of evil genius. – christopher Jun 02 '15 at 15:08
  • 1
    or evil idiot since it turns out I can't even read properly. – christopher Jun 02 '15 at 15:08
1

Simple... Change your int i =1234; value to char[] using this...

char[] chars = ("" + i).toCharArray();

after this use a loop to find the min value and print that...

CoderNeji
  • 2,056
  • 3
  • 20
  • 31
0

If input is only 1 line:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
char[] c = in.readLine().toCharArray();
char result = c[0];
for (char i : c) result = Math.min(result, i);

or if you have long integer:

long l;
// ...
char[] c = String.valueOf(l).toCharArray();
char result = c[0];
for (char i : c) result = Math.min(result, i);
egor.zhdan
  • 4,555
  • 6
  • 39
  • 53