Skip to content

How to separate and rotate numbers in integer? [closed]

I have a small problem. I think it’s simple, but I don’t know, how to manage it properly.

I have this simple int:

JavaScript

and I want output to look like this, in this specific format.

JavaScript

I did it with an integer array, but I want only one integer input like this one, not an array.

Could some body help me? Is there any simple method to manage it of, without using loops?

Advertisement

Answer

Basically, the conversion of the int representing a date in some format into String should use divide / and modulo % operations, conversion to String may use String.format to provide leading 0.

The number starting with 0 is written in octal notation, where the digits in range 0-7 are used, so the literals like 010819 or 080928 cannot even be written in Java code as int because of the compilation error:

JavaScript

However, (only for the purpose of this exercise) we may assume that the acceptable octal numbers start with 01 or 02 then such numbers are below 10000 decimal.

Then the numeric base for division/modulo and the type of output (%d for decimal or %o for octal) can be defined:

JavaScript

Tests:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful