Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- ======================================
- NoteBook chapter 4: Loops
- Ex1: Repeated digit in a number
- ======================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- int number,tmp,d,counter=0;
- byte digit;
- Scanner s=new Scanner(System.in);
- //user input
- System.out.println("enter a number and a digit: ");
- number=s.nextInt();
- digit=s.nextByte();
- tmp=number; //keeping the original number intact
- while (tmp!=0){
- d= tmp%10; // d = last digit of the number
- if(d==digit)
- counter++; //increase counter when same digits
- tmp/=10; //get rid of the last digit
- }
- System.out.println("number: "+number+ "\tdigit: "+ digit + "\trepeats: "+counter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment