Advertisement
eranseg

Merged Number

Jul 28th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MergedNumber {
  4.     public static void main(String[] args) {
  5.         int num1, num2, newNum = 0, location = 1;
  6.         Scanner sc = new Scanner(System.in);
  7.         System.out.println("Please enter first number: ");
  8.         num1 = sc.nextInt();
  9.         System.out.println("Please enter the second number. " +
  10.                            "Make sure the number of digits is equal" +
  11.                            " to the number of digits of the first number: ");
  12.         num2 = sc.nextInt();
  13.         while(num2 > 0) {
  14.             newNum += (num2 % 10) * location;// Add right digit of second number to new number
  15.             location *= 10;//Update location to shift digits to the left
  16.             newNum += (num1 % 10) * location;// Add right digit of second number to new number
  17.             location *= 10;//Update location to shift digits to the left
  18.             num2 /= 10;//Omit right digit of the second number
  19.             num1 /= 10;//Omit right digit of the first number
  20.         }
  21.         System.out.printf("The new merged number is: %d", newNum); //Print the new number
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement