PROBLEM: A positive integer is said to be a palindrome with respect to base
b, if its representation in base b reads the same from left to right as
from right to left. Palindromes are formed as follows:
Given a number, reverse its digits and add the resulting number to the original number. If the result isn't a palindrome, repeat the process. For example, start with 87 base 10. Applying this process, we obtain:
87 + 78 = 165
165 + 561 = 726
726 + 627 = 1353
1353 + 3531 = 4884, a palindrome
Whether all numbers eventually become palindromes under this process is unproved, but all base 10 numbers less than 10,000 have been tested. Every one becomes a palindrome in a relatively small number of steps (of the 900 3-digit numbers, 90 are palindromes to start with and 735 of the remainder take fewer than 5 reversals and additions to yield a palindrome). Except, that is, for 196. Although no proof exists that it will not produce a palindrome, this number has been carried through to produce a 2 million-digit number without producing a palindrome.
Given a number, reverse its digits and add the resulting number to the original number. If the result isn't a palindrome, repeat the process. For example, start with 87 base 10. Applying this process, we obtain:
87 + 78 = 165
165 + 561 = 726
726 + 627 = 1353
1353 + 3531 = 4884, a palindrome
Whether all numbers eventually become palindromes under this process is unproved, but all base 10 numbers less than 10,000 have been tested. Every one becomes a palindrome in a relatively small number of steps (of the 900 3-digit numbers, 90 are palindromes to start with and 735 of the remainder take fewer than 5 reversals and additions to yield a palindrome). Except, that is, for 196. Although no proof exists that it will not produce a palindrome, this number has been carried through to produce a 2 million-digit number without producing a palindrome.
Input: five base 10 positive integers
Output:Print the palindrome produced. If no palindrome is produced after 10 additions, print the word “none” and the last sum.
Solution:
import java.util.*;
public class Palindrome{
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int n1, n2, r = 0, c = 0;
for (int i = 0; i < 10; i++) {
System.out.println("enter a num");
n1 = sc.nextInt();
boolean check = true;
c = 0;
r = 0;
while (check) {
if (c < 10) {
r = 0;
n2 = n1;
while (n2 != 0) {
int d = n2 % 10;
n2 = n2 / 10;
r = (r * 10) + d;
}
if (r == n1) {
check = false;
} else {
n1 = n1 + r;
}
} else {
check = false;
n1 = n1 - r;
}
c++;
}
if (c >= 10) {
System.out.println("NONE"+n1);
} else {
System.out.println(n1);
}
}
}
}
public class Palindrome{
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int n1, n2, r = 0, c = 0;
for (int i = 0; i < 10; i++) {
System.out.println("enter a num");
n1 = sc.nextInt();
boolean check = true;
c = 0;
r = 0;
while (check) {
if (c < 10) {
r = 0;
n2 = n1;
while (n2 != 0) {
int d = n2 % 10;
n2 = n2 / 10;
r = (r * 10) + d;
}
if (r == n1) {
check = false;
} else {
n1 = n1 + r;
}
} else {
check = false;
n1 = n1 - r;
}
c++;
}
if (c >= 10) {
System.out.println("NONE"+n1);
} else {
System.out.println(n1);
}
}
}
}
0 Comments