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

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
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.
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT