Here's my code for the Palindrome problem: How can I reduce the execution time?
#include <stdio.h>
int numberofdigits(long x) {
int i=0;
while (!(x==0)) {
i++;
x=x/10;
}
return i;
}
int isPalindrome(long x) {
int pal=1;
int digits=numberofdigits(x);
int array[digits];
for (int j=0; j<digits; j++) {
array[digits-j-1] = x%10;
x=x/10;
}
//for even
if (digits % 2 == 0) {
for (int j=0; j<digits/2 ; j++) {
if (!(array[j] == array[digits-j-1])) {
//printf("%i =! %i, hence not a palindrome\n",array[j],array[digits-j-1]);
pal = 0;
break;
}
}
}
//for odd
else if (digits%2==1) {
for (int j=0; j<digits/2 ; j++) {
if (!(array[j] == array[digits-j-1])) {
//printf("%i =! %i, hence not a palindrome\n",array[j],array[digits-j-1]);
pal=0;
break;
}
}
}
return pal;
}
int main(int argc, const char * argv[]) {
// insert code here...
int cases;
scanf("%i",&cases);
long array[cases];
for (int i=0; i<cases; i++) {
scanf("%li",&array[i]);
}
for (int j=0; j<cases; j++) {
long input = array[j];
long counter=0;
while (!(isPalindrome(input+counter)== 1)) {
counter++;
}
printf("%li\n",counter+input);
}
}