Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MergedNumber {
- public static void main(String[] args) {
- int num1, num2, newNum = 0, location = 1;
- Scanner sc = new Scanner(System.in);
- System.out.println("Please enter first number: ");
- num1 = sc.nextInt();
- System.out.println("Please enter the second number. " +
- "Make sure the number of digits is equal" +
- " to the number of digits of the first number: ");
- num2 = sc.nextInt();
- while(num2 > 0) {
- newNum += (num2 % 10) * location;// Add right digit of second number to new number
- location *= 10;//Update location to shift digits to the left
- newNum += (num1 % 10) * location;// Add right digit of second number to new number
- location *= 10;//Update location to shift digits to the left
- num2 /= 10;//Omit right digit of the second number
- num1 /= 10;//Omit right digit of the first number
- }
- System.out.printf("The new merged number is: %d", newNum); //Print the new number
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement