Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PalindromeBase
import java.io.*;
public class PalindromeBase {
static String number = "";// this string is to store the reminder untill num/base == 0
// I used recursive function
public static int calculate(int num , int base){
if(num/base==0){
number += Integer.toString(num%base);
return 0;
}else{
number += Integer.toString(num%base);
int newNum = num / base;
return calculate(newNum,base);
}
}
// this function checks if the number which is in string form a palindrome or not
public static boolean isPalindrome(String numberAsString){
char[] array = numberAsString.toCharArray();
int e = array.length-1;
boolean checker = false;
for(int s = 0 ; s < array.length ; s++ , e--){
if(array[s]== array[e]){
checker = true;
}else{
checker = false;
break;
}
}
return checker;
}
// get function will take each line in the file and return the number and base as int type
public static int[] get(char[] line){
String ab = new String(line);
String[] splitter = ab.split("\\s");
int num1 , num2 ;
num1 = Integer.parseInt(splitter[0]);
num2 = Integer.parseInt(splitter[1]);
int[] returing = {num1,num2};
return returing;
}
public static void main(String[] args) throws Exception {
String str;
File file = new File("//Users//yarasaleh//Desktop//PalindromeBase//Test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
// while will loop through each line in the file
while((str =br.readLine() ) != null){
number = "";
char[] line = str.toCharArray();
// req array will store the number and base since functions in java cannot return two integers
int[] req = get(line);
int num , base;
num = req[0];
base = req[1];
if(num == 0 && base == 0){
break;
}else{
int g = calculate(num,base);
if(isPalindrome(number))
System.out.println("Yes");
else
System.out.println("No");
}
}
}
}
25 10
23 3
33 10
33 8
23 3
20 16
5 2
0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment