Advertisement
hpilo

Chapter4_Loops_Ex3

Dec 15th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3.     =====================================================
  4.                    chapter 4: Loops
  5.                    
  6.       Ex3: Display counter for same value & index
  7.     =====================================================
  8. */
  9.  
  10. public class MyProgram {
  11.     public static void main(String[] args) {
  12.        
  13.         //variables
  14.         int num1,num2;
  15.         int tmp1,tmp2;
  16.         int count=0;
  17.        
  18.         //user input
  19.         Scanner s=new Scanner(System.in);
  20.         System.out.println("Enter 2 numbers: ");
  21.         num1=s.nextInt();
  22.         num2=s.nextInt();
  23.        
  24.         //keeping the number intact
  25.         tmp1=num1;
  26.         tmp2=num2;
  27.  
  28.         while(tmp1>0){
  29.             if(tmp1%10==tmp2%10)
  30.                 count++;
  31.             tmp1/=10;
  32.             tmp2/=10;
  33.         }
  34.         System.out.println(count+" digits that have the same value & index");
  35.        
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement