Determining how many digits in an Integer are Odd Numbers

Write a Java program that will prompt the user for an integer.

The program will determine how many digit of that integer are odd.

The numbers 0, 2, 4, 6, and 8 are even digits, and 1, 3, 5, 7, 9 are odd digits.

You may not use a String to solve this problem, else receive zero credit for this assignment.

Use the modulo operator % to get the rightmost digit, and / to discard the rightmost digit.

A loop should be used in order to get from step 8 to step 3 in the roadmap.

Roadmap:

After prompting the user and reading an integer from the keyboard, your program must:

1. Name the number from the user input.

2. Declare and initialize a counter variable to zero.

3. Save the right-most digit of input in a variable using the modulo operator: rightMost = input % 10

4. Update the value of input = input / 10

5. Determine if the rightMost digit is odd. Use a divisibility test using the modulo operator %.

6. if rightMost is odd, increase the counter by 1.

7. if rightMost is even, do nothing.

8. if input is not zero, go to step 3.

9. if input is zero, the program has reached the last digit and it needs to go to step 10.

10. Display the results. counter will have the value of the number of odd digits in the original number.

* Your Java program should ask for and input the integer value to perform the operations on. Here are three sample runs:

Please enter an integer: 4822116
Number of odd digits: 2

Please enter an integer: 2448
Number of odd digits: 0

Please enter an integer: -7004
Number of odd digits: 1

Note:

  • Do not use any custom methods, Strings, or arrays in this assignment
  • You are not allowed to use the word "break" anywhere in your code.

Objectives:

  • To give you practice: Using methods from java.util.* to read keyboard input.
  • Using and understanding arithmetic operators, especially integer division and modulus.
  • Using and understanding conditional statements and loops.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.