'How to find the product and sum of digits of a number in java?

I need to find the sum and the product of the digits of the number entered. I have the sum part down but the only problem with it is that when I enter a number for the first time it gives me the right answer but when I enter another number, it just adds the sum of the first number and the sum of the second number together and then checks if the sum of the two sums is odd(sorry if its kind of confusing). For the product part of the code I just can't seem to figure out how to do it.

Finally if the sum and the product both are odd I need it to say that the number entered is an Extremely odd number.

Here is the assignment:

This Java application checks whole numbers in the interval [101, 100001] to see if they are "extremely odd" and "super extremely odd" numbers.

A whole number is "extremely odd" if...

(0) it's an odd number (1) it has an odd number of digits (2) all of its digits are odd numbers (3) the sum of its digits is an odd number (4) the product of its digits is an odd number

A "super extremely odd" number is an extremely odd number such that...

(5) all digits comprising the sum of its digits are odd numbers (6) all digits comprising the product of its digits are odd numbers

Loop prompting the user to enter positive whole numbers. Looping ends when a -1 is entered. For each number entered, check to see if its extremely odd (or super extremely odd).

If somebody could just explain what the requirements for the super extremely odd numbers part are saying that would be appreciated because English is not my first language. I don't need anyone to do the whole assignment. I just need help on the parts that I'm stuck in. Thank you in advance!

public class TestOddNumbers {

  public static void main(String[] args) {

    Scanner stdin = new Scanner(System.in);
    int userInput;
    int sum = 0;
    int product = 1;
    final int EXIT = -1;
    final int MIN = 101;
    final int MAX = 100001;
    String total = "";

    do{
        System.out.println("Please enter a positive whole number between "
                + "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
                + "when you are done entering numbers.");

        userInput = stdin.nextInt();
        total += String.valueOf(userInput);

        if(userInput==EXIT){
        System.out.println("Thanks for playing!");
        break;
        }

        if(userInput > MIN && userInput < MAX){
            if(userInput % 2 == 1){
            System.out.println("It's an odd number");
            }
            else{
                System.out.println("Not an odd number");
            }

            if(total.length() %2 == 1){
            System.out.println("It also has an odd number of digits");
            }
            else{
            System.out.println("Does not have an odd number of digits");
            }

            boolean[] allOdds = {true};

            String.valueOf(userInput).chars().forEach(i -> {
            if(Integer.parseInt(String.valueOf((char)i))%2==0){
            allOdds[0] = false;
            }
            });

            if(allOdds[0]){
                System.out.println("all digits are odds");
            }
            else{
                System.out.println("all digits are not odds");
            }

            // The sum code block
            while (userInput != 0) {
                sum += userInput % 10;
                userInput /= 10;
            }
            if((sum += userInput) % 2 == 1){
                System.out.println("The sum of the digits are odd");
            }
            else{
                System.out.println("The sum of the digits are not odd");
            }

            // The product code block
            while(userInput != 0) {
                product *= userInput % 10;
                userInput /= 10;
            }
            if((sum *= userInput) % 2 == 1){
                System.out.println("The product of the digits are odd");
            }
            else{
                System.out.println("The product of the digits are not odd");
            }

        }
        else{
            System.out.println("Check the bounds again!!");
        }

        System.out.println();
    } 
    while(userInput > MIN || userInput < MAX);

   }
}


Solution 1:[1]

public class Product_of_Digits_of_a_Number
{
    // accepting a three digit number
    static void calc(int n)
    {
        System.out.println("\f"); // clearing the screen

        // extracting each digit from the number
        int units = n % 10; // taking the units' digit out of the number and storing in u

        int tens = (n / 10) % 10; //taking tens' digit and string in tens.

        int hundreds = (n / 10) / 10; //taking hundreds' digit and storing in hundreds

        // performing the required calculation on the digits
        int a = units * tens * hundreds; // storing the product in a

        // printing the digits
        System.out.println("the product of digits of " + n + " is " + a);
   }
}

Note

If you want the sum, the code is the same but you have store the sum in variable a.

Solution 2:[2]

public class MyClass {
    static void myFun(int n)

    {
        int sum = 0;
        int prod = 1;
        while (n != 0)
        {
            sum = sum + n % 10;
            prod = prod * (n % 10);
            n = n / 10;
        }
        System.out.print("Product and Sum of digits of number given is:"+prod+" and " +sum);
    }
    public static void main(String[] a) {
        myFun(25);
    }
}

Source

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stefanobaghino
Solution 2 Joseph Samuel