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.

Respuesta :

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 );;

   

   

}

}

Explanation:

  • Divide the que variable by 10 and assign its result to the temp variable.
  • Calculate the remainder.
  • Increment the count if the remainder is equal to the value of D variable and assign the value of que to temp variable.