0

I'm 100% new to java. Cannot use string or "breaks" or arrays to solve the problem. Any feedback is great :)

Roadmap is:

  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 the input is not zero, go to step 3.

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

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

My code is below: (I probably haven't made it far, but I'm trying)( i likely might need to do a while or do while method)( any guide that has !, =< as a list would be appreciative)

import java.util.Scanner;

public class Labtwo 
{

    public static void main(String[] args) 
    {
        System.out.print("Please enter an integer:");// program lets user know to input a number
        // allow keyboard access for user
        // likely in a while or do while method?
        Scanner kbd = new Scanner(System.in); // allows user input labels it as kbd
        int input = kbd.nextInt();// kbd new value is input as a integer
        //int counter = 0;
        int rightMosteven= 0;
        int rightMostodd = 0;
        
        
        while (input > 0){
                int rightMost = (input % 10);
                if(rightMost%2==0)
                 rightMosteven ++;
                else
                rightMostodd++;
                input=input/10;
        }
        
        System.out.printf("Number of odd digits: "+ rightMostodd);
        //System.out.printf("Number of even digits: "+ rightMosteven);
        
    }

}

This is my new code updated im just not sure if == is considered a string if it is then my answer is 0 as stated by the "cannot use section"

2
  • 2
    What's your question? I see your homework assignment, and the beginnings of your code, but I have no idea what your actual question is? Commented Jun 28, 2021 at 3:51
  • my question is whether or not my code is following the roadmap, just updated the code Commented Jun 28, 2021 at 12:30

1 Answer 1

6

This answer assumes that you want to count the number of odd digits which some input number has. In fact, using the modulus is one viable way to do this. Consider this version:

Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int counter = 0;

while (input > 0) {
    if (input % 2 != 0) {
        ++counter;
    }

    input /= 10;
}

Here is a breakdown of what would happen for an example input of 12345:

1234  => last digit even, counter = 0
         divide input by 10
123   => last digit odd, counter = 1
         divide input by 10
12    => last digit even, counter = 1
         divide input by 10
1     => last digit odd, counter = 2
         divide input by 10
0     => terminate while loop
Sign up to request clarification or add additional context in comments.

2 Comments

it should be input/=10 not counter /= 10;
@sittsering Thanks for the correction (I already said this, but apparently some malicious moderator deleted my earlier comment).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.