In: Computer Science
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
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: