Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class HW3_ex02 {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- System.out.println("Enter two numbers with CORRESPONDING digits count:"); //ask user for a number
- int userNum1 = s.nextInt(); //get a number from the user
- int userNum2 = s.nextInt();
- int temp1 = userNum1; //set a temporary number1
- int temp2 = userNum2; //set a temporary number2
- int resultNumber = 0; //set the result number
- int position = 1; //set the position
- while (temp1 != 0) // while temp1 is not zero (we still have numbers inside)
- {
- resultNumber += temp2 % 10 * position; //add the last digit of number2 to it's position in result
- position *= 10; //move the position next by ones->tens->hundreds->...
- temp2 /= 10; // take out the last digit of num2
- resultNumber += temp1 % 10 * position; //add the last digit of number1 to it's position in result
- position *= 10; //move the position next by ones->tens->hundreds->...
- temp1 /= 10; // take out the last digit of num1
- // note we start from num2 then num1, position move for the entire result number.
- }
- System.out.println("Result:" + resultNumber); // print the result, combined number
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement