Search code examples
javamathdigit

How can I get each digit of a 16 digit number and store each digit as a variable?


I need to write a Java program that validates credit card numbers, and to do that I need to preform operations on each individual digit. How can I get these digits, without using strings or arrays?


Solution

  • int number; (This would be your credit card number)
    
    while (number > 0) {
        System.out.println( number % 10);
        number = number / 10;
    }
    

    This will print them in reverse order. You can perform your operations on the digits this way.