daily pastebin goal
84%
SHARE
TWEET

Chapter4_Loops_Ex3

hpilo Dec 15th, 2018 (edited) 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top