-
-
Save anonymous/c8da7ff388454ffd4c4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class NumberPuzzle2 { | |
public static void main (String[]args){ | |
Scanner kb = new Scanner(System.in); | |
int answer = 0; | |
do { | |
System.out.println("1) Find two digit numnbers <= 56 with sums of digits >10"); | |
System.out.println("2) Find two digit number minus number reversed which equals sum of digits"); | |
System.out.println("3) Quit"); | |
System.out.print("> "); | |
answer = kb.nextInt(); | |
System.out.println(); | |
if (answer==1){ | |
puzzle1(); | |
System.out.println(); | |
} | |
if (answer==2){ | |
puzzle2(); | |
System.out.println(); | |
} | |
}while (answer !=3); | |
kb.close(); | |
} | |
public static void puzzle1(){ | |
//For loop for tens place | |
for (int a=0; a<6; a++){ | |
//For loop for ones place | |
for (int b=0; b<10; b++){ | |
if (a+b>10){ | |
System.out.println(a+b); | |
} | |
if (a==5&&b==6){ | |
break; | |
} | |
} | |
} | |
} | |
public static void puzzle2(){ | |
int a,b,c=10,d,reverse=0, remainder=0; | |
//For loop for tens place | |
for (a=1;a<10;a++){ | |
//For loop for ones place | |
for (b=0;b<10;b++){ | |
//Save our current number to restore later | |
d=c; | |
//Reverse Digits | |
while( c > 0 ){ | |
//use modulus operator to strip off the last digit | |
remainder = c % 10; | |
//create the reversed number | |
reverse = reverse*10 + remainder; | |
c = c/10; | |
} | |
c=d; | |
if (c-reverse==a+b){ | |
System.out.println(c); | |
} | |
//Increase c to follow the single digits of a and b | |
c++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment