Advertisement
hpilo

Chapter4_Loops_Ex10

Dec 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3.     =====================================================
  4.                    chapter 4: Loops
  5.  
  6.       Ex10: partition number
  7.     =====================================================
  8. */
  9.  
  10.  
  11. public class MyProgram {
  12.     public static void main(String[] args) {
  13.         int num,digit,k,location=1;
  14.         int newNum=0;
  15.         int tmp,tmp2=0;
  16.         int partition,count=0;
  17.         Scanner s=new Scanner(System.in);
  18.  
  19.         System.out.println("Enter a number and a digit: ");
  20.         num=s.nextInt();
  21.         digit=s.nextInt();
  22.  
  23.         tmp=num;
  24.         k= (int) Math.pow(10,digit);
  25.         while(tmp>0){
  26.  
  27.             partition = tmp % k;
  28.             while(partition>0){
  29.                 tmp2*=10;
  30.                 tmp2+=partition%10;
  31.                 count++;
  32.                 partition/=10;
  33.  
  34.             }
  35.  
  36.                 newNum += location * tmp2;
  37.                 location *= Math.pow(10,count);
  38.                 tmp2=0;
  39.                 count=0;
  40.                 tmp /= k;
  41.         }
  42.         System.out.println(newNum);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement