hpilo

Chapter4_Loops_Ex1

Dec 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /*
  4.     ======================================
  5.         NoteBook chapter 4: Loops
  6.  
  7.         Ex1: Repeated digit in a number
  8.     ======================================
  9.  
  10. */
  11.  
  12. public class MyProgram {
  13.     public static void main(String[] args) {
  14.  
  15.         //variables
  16.         int number,tmp,d,counter=0;
  17.         byte digit;
  18.         Scanner s=new Scanner(System.in);
  19.  
  20.         //user input
  21.         System.out.println("enter a number and a digit: ");
  22.         number=s.nextInt();
  23.         digit=s.nextByte();
  24.  
  25.         tmp=number;     //keeping the original number intact
  26.  
  27.  
  28.         while (tmp!=0){
  29.             d= tmp%10;       // d = last digit of the number
  30.             if(d==digit)
  31.                 counter++;       //increase counter when same digits
  32.             tmp/=10;         //get rid of the last digit
  33.         }
  34.         System.out.println("number: "+number+ "\tdigit: "+ digit + "\trepeats: "+counter);
  35.  
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment