Advertisement
Guest User

Question 1 loops

a guest
Jun 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class q1 {
  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 number:");
  12.         int userNum= S.nextInt();   //the user enter number
  13.        
  14.         int indexNewNum= 1;         // installed indexer to the result number
  15.        
  16.         int newNum=0;               // installed the result number
  17.        
  18.         int num=userNum;            // installed the helper variable <-- userNum
  19.        
  20.         final int MOOD=2;           // installed the number of the division <-- 2
  21.        
  22.    
  23.        
  24.         while (num>0) {         // run as long as num up to 0
  25.            
  26.                 int moodNum= num%10;    // installed helper variable <-- the right digit
  27.                        
  28.             if(moodNum%MOOD==0){        // if the left digit is double
  29.                
  30.                 newNum+=indexNewNum*moodNum;    //the right digit add to the result number to the left
  31.                
  32.                 indexNewNum*=10;        // the indexer double by 10
  33.            
  34.             }
  35.            
  36.             num/=10;        // remove the right digit
  37.            
  38.            
  39.         }
  40.        
  41.         System.out.println("the new number is: "+newNum); // print the result
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement