I
I
Ilya Balabanov2015-10-11 22:25:39
Java
Ilya Balabanov, 2015-10-11 22:25:39

How to implement this example?

Good day! I was given an assignment at the university. Implement an algorithm for gradual division of an arbitrary number by 10 with a remainder. For example, we divide 123 by ten, we get the integer part - 12, the fractional part - 3 (we remember it). Next, we divide 12 (the integer part) by 10 and again remember the fractional part, and divide the whole again. Until it is equal to zero. If you write the fractional parts in the line after division, you get 321 (reversed 123 - the original number). I am attaching some work. I can not actually generalize the algorithm for any number. I would appreciate any advice, thanks.

int input = 123; // произвольное число
        int in = input/10; //целая часть
        int f = input%10; //дробная часть
        String[] ar = new String[3];
        do {
            int v1 = in/10;
            for(int i=0; i<=2; i++){
               ar[i] = Integer.toString(in%10);
            }
        } while(in == 0);
        for(int i = 0; i <  ar.length; i++) {
            System.out.print(ar[i] + "");
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Afganec, 2015-10-11
@ilyablbnv

Hello. What is wrong now? Not quite sure what answer you're expecting :)

M
Maxim Moseychuk, 2015-10-11
@fshp

Use a list instead of an array, or use the logarithm to find out in advance the number of decimal places in a number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question