Skip to content

Instantly share code, notes, and snippets.

@yaraxxx
Created February 2, 2020 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaraxxx/b7b5013e7b56fcbecdd79fadabb4051c to your computer and use it in GitHub Desktop.
Save yaraxxx/b7b5013e7b56fcbecdd79fadabb4051c to your computer and use it in GitHub Desktop.
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