Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class hw1 {
- public static void main(String[] args) {
- int loc = 1, newnum = 0, x = 0;
- Scanner s = new Scanner(System.in);
- System.out.println("Enter a number:");
- x = s.nextInt();//getting a number from user
- int temp = x;//saving the number in temp for later
- while (temp > 0) {//if temp is not 0 keep working
- if ((temp % 10) % 2 == 0) {//taking the right digit and checking if even
- newnum += loc * (temp % 10);//taking the right digit and adding it to the new number we want
- loc *= 10;//multiplying loc for next digit
- }
- temp /= 10;//removing the last right digit we used
- }
- System.out.println("New number is " + newnum);//result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment