0

How do I put each number in a variable, for example, I have 2118 and I want this:

How do I do this if the number is unknown?

int abcd = 2118;
int a = 8;
int b = 10;
int c = 100;
int d = 2000;

Or

int abcd = 2118;
int a = 8; // 8
int b = 1; // 10
int c = 1; // 100
int d = 2; //2000
Zippy
  • 3,826
  • 5
  • 43
  • 96
Deaak
  • 13
  • 6
  • what do you mean? because you already put your numbers in a variable – Kristofer Mar 30 '18 at 23:03
  • How do I do this if the number is unknown? – Deaak Mar 30 '18 at 23:06
  • Will it always be 4 digits? – cpp beginner Mar 30 '18 at 23:06
  • Not from 0-million – Deaak Mar 30 '18 at 23:09
  • This does not seem a question based on android or android-studio but it is java-specific. There is no code example, so this is even not a technical question but you search a solution with no any tries. Can you at least provide a basic code of yours so that people can fix it? – Levent Divilioglu Mar 30 '18 at 23:15
  • Please try to do it yourself and ask a question about a specific problem you have with your code. If you don't know enough Java/don't have enough programming skills to do that, please learn Java/programming. – Oleg Mar 30 '18 at 23:24
  • I did not write the code in this regard because I do not know how to begin But to clarify: The user may enter a number that is perhaps 0 and possibly greater than one million and want to sort the number according to each number in a variable – Deaak Mar 30 '18 at 23:24
  • @kaaed you want to break the number down into ones, tens, hundreds etc... correct? – Zippy Mar 30 '18 at 23:27
  • Duplicate of https://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number – Oleg Mar 30 '18 at 23:33

3 Answers3

1

I would use the modulus operator to do what (I think) you are asking.

Something like this...

 int num=1351280;
 int ones = (num % 10)/1;
 int tens = (num-ones) % 100;
 int hun = (num-(tens+ones)) % 1000;
 int thou = (num-(tens+ones+hun)) % 10000;
 int tenThou = (num-(tens+ones+hun+thou)) % 100000;
 int hunThou = (num-(tens+ones+hun+thou+tenThou)) % 1000000;
 int mil  = (num-(tens+ones+hun+thou+tenThou+hunThou)) % 10000000;

 System.out.println(mil);
 System.out.println(hunThou);
 System.out.println(tenThou);
 System.out.println(thou);
 System.out.println(hun);
 System.out.println(tens);
 System.out.println(ones);

This gives:

1000000 (Millions)
300000  (Hundred thousands)
50000   (Ten thousands)
1000    (Thousands)
200     (Hundreds)
80      (Tens)
0       (Ones)

The modulus operator gives you the remainder so 30 % 8 = 6 which is something like (30/8) = 3. And 30-(3*8) = 6;

Zippy
  • 3,826
  • 5
  • 43
  • 96
  • 1
    Thank you very much already solved my problem and learned something new from you Thank you from my heart . – Deaak Mar 30 '18 at 23:48
0

Do you can convert to String then use String#toCharArray() and finally do a reverse loop.

String num = String.valueOf(someInt);
char[] dig = num.toCharArray();
char[] reverse = new char[dig.length()];
int j = 0;
for(int i=reverse.length-1; i>=0; i--) {
    reverse[j] = dig[i];
    j++;
}
Marks
  • 53
  • 6
0

You should prefer calculating digits recursively;

Sample Code

package com.levo.so.digits;

public class DigitDisplay {

    public static void main(String[] args) {
        int number = 2118;

        System.out.println("Number : " + number);
        System.out.println();

        int[] digits = getDigitArray(number);


        System.out.println("Digits;");
        System.out.println("-------");
        for(int i = 0; i < digits.length; i++) {
            System.out.printf("Digit [%d]: %d\n", i, digits[i]);
        }

        System.out.println("\n");

        System.out.println("Components");
        System.out.println("----------");
        for(int i = 0; i < digits.length; i++) {
            System.out.printf("[%d]: %d\n", i, digits[i]*(int)Math.pow(10, i));
        }
    }

    private static int[] getDigitArray(int number) {
        int[] array = new int[getDigitCount(number)];

        int index = 0;
        while(number != 0) {
            array[index++] = (number%10);
            number /= 10;
        }

        return array;
    }

    private static int getDigitCount(int number) {
        int digitCount = 0;

        while(number != 0) {
            number /= 10;
            digitCount++;
        }

        return digitCount;
    }

}

Output

Number : 2118

Digits;
-------
Digit [0]: 8
Digit [1]: 1
Digit [2]: 1
Digit [3]: 2


Components
----------
[0]: 8
[1]: 10
[2]: 100
[3]: 2000
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106