Find numbers which are palindrome in both their decimal and octal representations. | Algorithms
by SJ · February 22, 2015 Objective: Given a range of integers, find all the numbers which are palindrome when they are represented in Decimal Value( base 10) and in Octal value(base 8). Example : Number : 373 (Decimal) and digits are palindrome. Convert it into Octal which is 565 and that's also palindrome. Approach: Solution is quite simple. Traverse through all the numbers in the given range and check if it palindrome, if yes, convert it into Octal and check for palindrome again. Code: public class DecimalOctalPalindrome { public String DecimalToOctal(int N) { String Oct = ""; while (N > 0) { int x = N % 8; N = N / 8; Oct += x; } return Oct; } public boolean isPalindrome(String S) { int i = 0; int j = S.length() - 1; while (i < j) { if (S.charAt(i) != S.charAt(j)) { return false; } i++; j--; } return true; } public void findBothPalindrome(int start, int end) { for (int i = start; i <= end; i++) { String decimal = String.valueOf(i);Read full article from Find numbers which are palindrome in both their decimal and octal representations. | Algorithms
No comments:
Post a Comment