chaibs

loops-hw2

Dec 27th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class hw2 {
  4.  
  5.     public static void main(String[] args) {
  6.         int num1 = 0, num2 = 0, newNum = 0, loc = 1;
  7.  
  8.         Scanner s = new Scanner(System.in);
  9.  
  10.         System.out.println("Please enter the first number:");
  11.         num1 = s.nextInt();// getting first number from user
  12.  
  13.         System.out.println(
  14.                 "Please enter the second number(The number must have the same number of digits as the first number):");
  15.         num2 = s.nextInt();// getting second number from user
  16.  
  17.         while (num2 > 0) {// as long as the num2 bigger then 0 continue
  18.             newNum += loc * (num2 % 10);// adding and multiplying the most right digit from the second number
  19.             loc *= 10;// multiplying loc in 10
  20.             newNum += loc * (num1 % 10);// adding and multiplying the most right digit from the second number
  21.             loc *= 10;// multiplying loc in 10
  22.             num2 /= 10;// removing the right digit from the second number
  23.             num1 /= 10;// removing the right digit from the second number
  24.         }
  25.  
  26.         System.out.println("The new number is " + newNum);// result
  27.  
  28.     }
  29.  
  30. }
Advertisement
Add Comment
Please, Sign In to add comment