Advertisement
Guest User

Untitled

a guest
May 27th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner s = new Scanner(System.in);
  7.         int num1,num2,temp1,temp2,finalnum=0,count1=1,count2=1;
  8.  
  9.         System.out.println("Enter 2 numbers with eqal lenght");
  10.         num1 = s.nextInt();
  11.         num2 = s.nextInt();
  12.  
  13.         temp1=num1;//*set new int that store the original numbers..
  14.         temp2=num2;//*so we can run tests without losing them
  15.  
  16.         while(temp1/10!=0){
  17.             count1++;
  18.             temp1/=10;
  19.         }             //*we /10 the numbers and add 1 to count as long as it happens
  20.         while(temp2/10!=0){
  21.             count2++;
  22.             temp2/=10;
  23.         }
  24.  
  25.             if(count1==count2){ //* we check if the numbers are equal in length by comparing the counts
  26.                 count1=1;//*if Condition is met we set the count in position 1
  27.                 while(num2!=0){//*as long as num2 its not zero we have numbers left in it
  28.                     finalnum+=(num2%10)*count1;//*we build the number from right to left
  29.                     count1*=10;//*change the position by 10
  30.                     num2=num2/10;//* /10 the number to keep going until its 0
  31.  
  32.                     finalnum+=(num1%10)*count1;
  33.                     count1*=10;             //*same process as we did to num2
  34.                     num1=num1/10;
  35.                 }
  36.             }
  37.             else {
  38.                 System.out.println("The numbers was not equal in length");
  39.                 //* if the counts we set wasnt equal the user didnt follow the instructions
  40.             }
  41.                 System.out.println("NEW number is :"+finalnum);
  42.                 //* we print the new mixed number
  43.  
  44.  
  45.         }
  46.  
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement