chaibs

loops-hw1

Dec 27th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class hw1 {
  4.  
  5.     public static void main(String[] args) {
  6.         int loc = 1, newnum = 0, x = 0;
  7.  
  8.         Scanner s = new Scanner(System.in);
  9.  
  10.         System.out.println("Enter a number:");
  11.         x = s.nextInt();//getting a number from user
  12.  
  13.         int temp = x;//saving the number in temp for later
  14.  
  15.         while (temp > 0) {//if temp is not 0 keep working
  16.             if ((temp % 10) % 2 == 0) {//taking the right digit and checking if even
  17.                 newnum += loc * (temp % 10);//taking the right digit and adding it to the new number we want
  18.                 loc *= 10;//multiplying loc for next digit
  19.             }
  20.             temp /= 10;//removing the last right digit we used
  21.         }
  22.         System.out.println("New number is " + newnum);//result
  23.     }
  24.  
  25. }
Advertisement
Add Comment
Please, Sign In to add comment