hpilo

Chapter4_Loops_Ex9

Dec 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3.     =====================================================
  4.                    chapter 4: Loops
  5.  
  6.       Ex9: new number only 9 digits
  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 countDig,digit;
  17.         int location=1;
  18.         int maxDigit=0;
  19.         int res=0;
  20.         Scanner s=new Scanner(System.in);
  21.        
  22.         //user input
  23.         System.out.println("Enter 2 numbers with the same number of digits: ");
  24.         num1=s.nextInt();       //number-digits for counter
  25.         num2=s.nextInt();      // number- digits to repeat
  26.        
  27.         //keeping the numbers intact
  28.         tmp1=num1;
  29.         tmp2=num2;
  30.        
  31.         while(tmp1>0 && maxDigit<9){
  32.             countDig=tmp1%10;
  33.             digit=tmp2%10;
  34.             for(int i=0;i<countDig;i++)
  35.             {
  36.                 if(maxDigit<9) {
  37.                     res += digit * location;
  38.                     location *= 10;
  39.                     maxDigit++;
  40.                 }
  41.             }
  42.             tmp1/=10;
  43.             tmp2/=10;
  44.         }
  45.         System.out.println(res);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment