Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class hw2 {
- public static void main(String[] args) {
- int num1 = 0, num2 = 0, newNum = 0, loc = 1;
- Scanner s = new Scanner(System.in);
- System.out.println("Please enter the first number:");
- num1 = s.nextInt();// getting first number from user
- System.out.println(
- "Please enter the second number(The number must have the same number of digits as the first number):");
- num2 = s.nextInt();// getting second number from user
- while (num2 > 0) {// as long as the num2 bigger then 0 continue
- newNum += loc * (num2 % 10);// adding and multiplying the most right digit from the second number
- loc *= 10;// multiplying loc in 10
- newNum += loc * (num1 % 10);// adding and multiplying the most right digit from the second number
- loc *= 10;// multiplying loc in 10
- num2 /= 10;// removing the right digit from the second number
- num1 /= 10;// removing the right digit from the second number
- }
- System.out.println("The new number is " + newNum);// result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment