This assignment will give you more experience on the use of loops In this projec
ID: 3543207 • Letter: T
Question
This assignment will give you more experience on the use of loops
In this project, we are going to compute the number of times a given digit D appears in a given number N. For example, the number of times 5 appears in 1550 is 2. The number of times 0 appears in 1550 is 1. The number of times 3 appears in 155 is 0. Etc.
Task
Your task is to implement the following the algorithm.
1- initialize a counter to 0
2- decompose the number N into its corresponding digits by calculating quotients and remainders of dividing it by 10
3- increment the counter each time the digit D appears
Example:
Given the number N = 1550 and the digit D = 5:
Calculated Digit
Counter
0
0
5
1
5
2
1
2
Given the number N = 96395 and the digit D = 9:
Calculated Digit
Counter
5
0
9
1
3
1
6
1
9
2
Project Description / Specification
1. Prompt the user for the given number and the given digit.
2. The program should have error checking to make sure the user inputs are valid. For example, if a user gives non-integer inputs, notify the user that the inputs are incorrect and prompt again.
3. Decompose the number in a loop and increment the counter within the loop as described in the example above.
Sample Interaction:
Calculated Digit
Counter
0
0
5
1
5
2
1
2
This assignment will give you more experience on the use of loops In this project, we are going to compute the number of times a given digit D appears in a given number N. For example, the number of times 5 appears in 1550 is 2. The number of times 0 appears in 1550 is 1. The number of times 3 appears in 155 is 0. Etc. Your task is to implement the following the algorithm. initialize a counter to 0 decompose the number N into its corresponding digits by calculating quotients and remainders of dividing it by 10 increment the counter each time the digit D appears Given the number N = 1550 and the digit D = 5: Given the number N = 96395 and the digit D = 9: Project Description / Specification Prompt the user for the given number and the given digit. The program should have error checking to make sure the user inputs are valid. For example, if a user gives non-integer inputs, notify the user that the inputs are incorrect and prompt again. Decompose the number in a loop and increment the counter within the loop as described in the example above.Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Tester {
public static void main(String[] args) throws IOException {
Scanner in=new Scanner(System.in);
System.out.println("Enter a Number =======>");
long N ;
while (!in.hasNextLong()) {
System.out.println("That's not a number!");
in.next();
}
N=in.nextLong();
System.out.println("Number Entered is =======>"+N);
System.out.println("Enter a Digit =======>");
int D;
while (!in.hasNextLong()) {
System.out.println("That's not a number!");
in.next();
}
D=in.nextInt();
System.out.println("Digit Entered is =======> "+ D);
long que=N;
int rem;
int count=0;
while(true){
long temp;
temp = (long)que/10;
rem = (int) (que % 10);
System.out.println(temp+" "+ que+" "+rem);
if(rem==D) count++;
que=temp;
if(que==0) break;
}
System.out.println("The number of "+ D+"'s in "+ N + " is "+ count );;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.