Advertisement
hpilo

Chapter4_Loops_Ex5

Dec 15th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3.     =====================================================
  4.                    chapter 4: Loops
  5.  
  6.       Ex5: change digits place
  7.     =====================================================
  8. */
  9.  
  10. public class MyProgram {
  11.     public static void main(String[] args) {
  12.        
  13.         //variables
  14.         int num,tmp,count=1;
  15.         int newNum=0;
  16.         int twoDig;
  17.         Scanner s=new Scanner(System.in);
  18.        
  19.         //user input
  20.         System.out.println("Enter a number: ");
  21.         num=s.nextInt();
  22.         tmp=num;    //keeping the num intact
  23.  
  24.         while(tmp/10>0){
  25.  
  26.             twoDig=tmp%100;
  27.             twoDig=(twoDig%10)*10+(twoDig/10);
  28.             newNum+= twoDig*count;
  29.  
  30.             tmp/=100;
  31.             count*=100;
  32.         }
  33.  
  34.         if(tmp>0)
  35.             newNum+=(tmp%10)*count;
  36.  
  37.         System.out.println("old: "+num+" new: "+newNum);
  38.  
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement