Advertisement
Guest User

Question 2 loops

a guest
Jun 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class q2 {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.  
  9.         Scanner S= new Scanner(System.in);
  10.        
  11.         System.out.println("enter the first number:");
  12.         int num1= S.nextInt();      //the user enter the first number
  13.        
  14.         System.out.println("enter the second number:");
  15.         int num2= S.nextInt();      //the user enter the second number
  16.        
  17.         int newNum1=num1, newNum2=num2; // installed the helper variable newNum1 <- num1 and newNum2 <- num2
  18.        
  19.         int newNum= 0;      // installed the result number
  20.        
  21.         int indexNewNum= 1;     // installed indexer to the result number
  22.        
  23.         while (newNum2 > 0) {   // run as long as newNum2 up to 0
  24.            
  25.             newNum+= newNum2%10*indexNewNum;    // add the right digit from the second number to the left of the result number  
  26.            
  27.             indexNewNum*= 10;           // the indexer double by 10
  28.            
  29.             newNum+= newNum1%10*indexNewNum;    // add the right digit from the first number to the left of the result number  
  30.            
  31.             indexNewNum*= 10;           // the indexer double by 10
  32.            
  33.             newNum1/=10;            //remove the right digit from first number
  34.            
  35.             newNum2/=10;            //remove the right digit from second number
  36.         }
  37.        
  38.        
  39.         System.out.println("the new number is: "+newNum);       // print the result
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement