The Next Palindrome
The below code is the solution to this problem
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
Output
For each K, output the smallest palindrome larger than K.
Example
Input: 2 808 2133
Output: 818 2222
#include <iostream>
using namespace std;
int main()
{
long t;
cin>>t;
long a[t],k;
for(long i=0;i<t;++i)
{
cin>>k;
a[i]=k;
}
for(long i=0;i<t;++i)
{
long palin=0,num;
palin=a[i];
num=palin+1;
while(1)
{
long x=0,rev=0,ans=num;
do
{
x=ans%10;
rev=rev*10+x;
ans=ans/10;
}while(ans);
if(rev==num)
{
cout<<"\n"<<rev<<"\n";
break;
}
else
++num;
}
}
return 0;
}
The code is giving me expected output, I even made changes in the code making the variable K and t to LONG, should i make them long long instead of long, or is there any issue with my logic ...?
You can write a effective program by using a string instead of long. I have used it. Here I am giving the code:
#include<iostream>
using namespace std;
#include<string.h>
int main()
{
char s[80]; //you can take any big index instead 80
gets(s);
int a=0,l=strlen(s);
for(int i=0;i<l;i++) {
if(s[i]==s[l-i]) {
a++;
}
}
if(a==l) {
cout<<"Number is palindrome";
}
else {
cout<<"Number is not palindrome";
}
return(0);
}