Prof. Ramish is working on the problem called The
196-Problem. The problem is as follows:
Step 1: Take any number with atmost 3 digits.
Step 2: Form a mirror of the
number choosen in step 1 by reversing it. Step 3: Add the number of step 1 and
its mirror derived in step 2.
It may happen that the sum calculated in step 3 is
palindromic number. A number N is palindromic if by reversing the digits of N
we get the same number N. If the sum calculated in step 3 is not palindromic
number then take the mirror of the sum and add it into the sum. Perhaps the now
the new sum may be palindromic number. If it is not then repeat the process
again.
At the end nearly all numbers lead to a palindromic number.
There may be some numbers which may not lead to a palindromic number.
- Can
you help Prof. Ramish by writing a program which takes a number with atmost 3
digits as an argument and find whether the input number leads to a palindromic
number or not? - If input number leads
to a palindromic number then also display in how many iteration it leads to a
palindromic number?
- If
input number does not lead to palindromic number after 7 iteration then display
"Number does not lead to palindromic number"
- If
user does not provide any number then display "Missing required
inputs" - If user provides a number with more than 3 digits long or less
than 2 digits long then display "Invalid number"
- If
user does not provide a valid number then also display "Invald
number"
Input
Command Line Argument: <<A number with 3 digits>>
Output
[Number leads to palindromic number in X iterations] or
[Number does not lead to palindromic number] or [Invalid number] or [Missing
required inputs]
Example
Inputs: Command Line Argument: 88
Output: Number leads to palindromic number in 6 iterations
/* Here the original number is 88.
Iteration 1: 88 + 88 = 176
Iteration 2: 176 + 671 = 847
Iteration 4: 1595 + 5951 = 7546
Iteration 5: 7546 + 6457 = 14003
Iteration 6: 14003 + 30041 = 44044
44044
is a palindromic number.
/*
Example 2:
Inputs: Command Line Argument: 196
Output: Number does not lead to palindromic number
/* Here the original number is 196
Iteration 1: 196 + 691 = 887
Iteration 2: 887 + 788 = 1675
Iteration 3: 1675 + 5761 = 7436
Iteration 4: 7436 + 6347 = 13783
Iteration 5: 13783 + 38731 = 94039
Iteration 6: 94039 + 93049 = 187088
Iteration 7: 187088 + 880781 = 1067869
Hence, even after 7
iteration number does not lead to palindromic number.
*/
solution
No comments:
Post a Comment