Advertisement
dev_start

Untitled

May 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HW3_ex02 {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner s = new Scanner(System.in);
  7.  
  8.         System.out.println("Enter two numbers with CORRESPONDING digits count:");  //ask user for a number
  9.         int userNum1 = s.nextInt();  //get a number from the user
  10.         int userNum2 = s.nextInt();
  11.  
  12.         int temp1 = userNum1;   //set a temporary number1
  13.         int temp2 = userNum2;  //set a temporary number2
  14.         int resultNumber = 0;  //set the result number
  15.         int position = 1;  //set the position
  16.  
  17.         while (temp1 != 0)  // while temp1 is not zero (we still have numbers inside)
  18.         {
  19.             resultNumber += temp2 % 10 * position;  //add the last digit of number2 to it's position in result
  20.             position *= 10;   //move the position next by ones->tens->hundreds->...
  21.             temp2 /= 10;  // take out the last digit of num2
  22.             resultNumber += temp1 % 10 * position;  //add the last digit of number1 to it's position in result
  23.             position *= 10;  //move the position next by ones->tens->hundreds->...
  24.             temp1 /= 10;  // take out the last digit of num1
  25.             // note we start from num2 then num1, position move for the entire result number.
  26.         }
  27.         System.out.println("Result:" + resultNumber); // print the result, combined number
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement