Question

In: Computer Science

Counts the number of odd, even, and zero digits in an integer input value. Repeat until...

Counts the number of odd, even, and zero digits in an integer input value. Repeat until user does not want to continue. Develop the program in an incremental fashion. For example, write the part of the program, which inputs a single integer value and displays number of odd, even, and zero digits in that number. Submit your partial program for grading to make sure it works for the first few test cases. Below is an example execution of a partial program: Enter an integer value: 22000333 The number 22000333 contains Zero digits: 3 Even digits: 2 Odd digits: 3 Now, you can embed your partial code into a loop, which will allow the user to input more than one integer number, one at a time, and to see the number of odd, even, and zero digits in each respective number. When the user enters the sentinel value -99, the program terminates. Refer to Section 4.3 of zyBooks for an example program, which uses a loop with sentinel value. Below is an example execution of a complete program: ********************************************* Name: ??, CSCI1301, Section ??, Term: ?? Count Digits ********************************************* Enter an integer value (-99 to end): 7 The number 7 contains Zero digits: 0 Even digits: 0 Odd digits: 1 Enter an integer value (-99 to end): 22000333 The number 22000333 contains Zero digits: 3 Even digits: 2 Odd digits: 3 Enter an integer value (-99 to end): 12345 The number 12345 contains Zero digits: 0 Even digits: 2 Odd digits: 3 Enter an integer value (-99 to end): -99 Have a nice day!

Program is java

Solutions

Expert Solution

Explanation: I have written both the partial code without including the loop and have provided the output and have also written the complete code by embedding the partial code inside the loop and have shown the output.I have kept the name of the class as CountDigit, you can change it according to your need.I have also shown the output of the program, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation

//partial code without loop

import java.util.Scanner;

public class CountDigit {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter a single integer value:");
       int number = input.nextInt();
       int copyNumber = number;
       int evenCount = 0, oddCount = 0, zeroCount = 0;
       while (copyNumber != 0) {
           int digit = copyNumber % 10;
           if (digit == 0)
               zeroCount++;
           else if (digit % 2 == 0) {
               evenCount++;
           } else
               oddCount++;
           copyNumber = copyNumber / 10;
       }
       System.out.println("The number " + number + " contains Zero digits: "
               + zeroCount + " Even digits: " + evenCount + " Odd digits: "
               + oddCount);

       input.close();

   }

}
Output:

//embedded partial code with loop

import java.util.Scanner;

public class CountDigit {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       while (true) {
           System.out.print("Enter an integer value (-99 to end): ");
           int number = input.nextInt();
           if (number == -99) {
               System.out.println("Have a nice day!");
               break;
           }
           int copyNumber = number;
           int evenCount = 0, oddCount = 0, zeroCount = 0;
           while (copyNumber != 0) {
               int digit = copyNumber % 10;
               if (digit == 0)
                   zeroCount++;
               else if (digit % 2 == 0) {
                   evenCount++;
               } else
                   oddCount++;
               copyNumber = copyNumber / 10;
           }
           System.out.println("The number " + number
                   + " contains Zero digits: " + zeroCount + " Even digits: "
                   + evenCount + " Odd digits: " + oddCount);

       }

       input.close();

   }

}

Output:


Related Solutions

//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input...
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input value. Repeat as long as the user wishes to continue //----------------------------------------------------------------- public static void main(String[] args) {     // Declare the identifiers final int SENTINEL = -99;    // Declare the remaining identifiers ... Scanner scan = new Scanner(System.in);    // Display the programmer's information              // Input an integer number          // Count the number of odd, even, and...
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without...
Using for loop . Input an integer and identify whether it's EVEN OR ODD ( without using modulo operator ) using only <iostream> . C++ programming
Create an application that checks whether an integer is an odd or even number. Welcome to...
Create an application that checks whether an integer is an odd or even number. Welcome to the Odd/Even Checker! Enter an integer: ten Error! Invalid integer. Try again. Enter an integer: 10.3 Error! Invalid integer. Try again. Enter an integer: 10 The number 10 is even. Continue? (y/n): Error! This entry is required. Try again. Continue? (y/n): y Enter an integer: 9 The number 9 is odd. Continue? (y/n): n Specifications: Create a version of the Console class presented in...
prove that if an even integer n is subtracted from an odd integer m. then m...
prove that if an even integer n is subtracted from an odd integer m. then m - n is odd.
prove that every integer is either even or odd but never both.
prove that every integer is either even or odd but never both.
Prove that every natural number is odd or even.
Prove that every natural number is odd or even.
Read four digits from the random numbers table. note the number of odd digits in the...
Read four digits from the random numbers table. note the number of odd digits in the selected numbers. Repeat the experiment 25 times. Obtain the probability distribution of the odd digits. Find mean and variance of the distribution
What is the probability that a randomly chosen four-digit integer has distinct digits and is odd?
What is the probability that a randomly chosen four-digit integer has distinct digits and is odd?
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT