Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- =====================================================
- chapter 4: Loops
- Ex9: new number only 9 digits
- =====================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- int num1,num2;
- int tmp1,tmp2;
- int countDig,digit;
- int location=1;
- int maxDigit=0;
- int res=0;
- Scanner s=new Scanner(System.in);
- //user input
- System.out.println("Enter 2 numbers with the same number of digits: ");
- num1=s.nextInt(); //number-digits for counter
- num2=s.nextInt(); // number- digits to repeat
- //keeping the numbers intact
- tmp1=num1;
- tmp2=num2;
- while(tmp1>0 && maxDigit<9){
- countDig=tmp1%10;
- digit=tmp2%10;
- for(int i=0;i<countDig;i++)
- {
- if(maxDigit<9) {
- res += digit * location;
- location *= 10;
- maxDigit++;
- }
- }
- tmp1/=10;
- tmp2/=10;
- }
- System.out.println(res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment