The Problem It is said that Albert Einstein used to take great delight in baffli
ID: 3542259 • Letter: T
Question
The Problem
It is said that Albert Einstein used to take great delight in baffling friends with the puzzle below.
First, write the number 1089 on a piece of paper, fold it, and hand it to a friend for safekeeping. What you wrote down is not to be read until you have completed your amazing mental feat.
Next, ask your friend to write down any three-digit number, emphasizing that the first and last digits must differ by at least two. Close your eyes or turn your back while this is being done. Better still, have someone blindfold you.
After your friend has written down the three-digit number, ask him to reverse it, then subtract the smaller from the larger.
Example: 654 - 456 = 198.
Once this is done, tell your friend to reverse the new number.
Example: 198 becomes 891.
Next ask your friend to add the new number and its reverse together.
Example: 198 + 891 = 1089.
If all goes as planned, your friend will be amazed. The number you wrote down at the start -- 1089 -- will always be the same as the end result of this mathematical trick.
Program Specifications
Your program will play the Einstein game as follows:
Sample Interaction
Explanation / Answer
import java.util.*;
class Puzzle
{
public static int reverse (int number)
{
int rev = 0;
while( number != 0 )
{
rev = rev * 10;
rev = rev + number%10;
number = number/10;
}
return rev;
}
public static void main (String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("This is a puzzle favoured by Einstien.You will be asked to enter a three digit number, where the hundred's digit differs from the one's digit by at least two. The procedure will always yield 1089");
System.out.println();
System.out.print("Give me a number:");
int n = s.nextInt();
int rn = reverse(n);
int diff = Math.abs(n-rn);
int rdiff=reverse(diff);
System.out.println("For the number: "+n+" the reverse number is: "+rn);
System.out.println("The difference between "+n+" and "+rn+" is "+diff);
System.out.println("The reverse difference is: "+rdiff);
System.out.println("The sum of: "+diff+" the revDiff is: "+(diff+rdiff));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.