CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(Peer) » Palindrome

Palindrome

Problem code: DPC206

  • Submit
  • All Submissions

All submissions for this problem are available.

Palindrome

 

          A simple recursive method to generate a numeric palindrome from any number is to reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure. For example for 195:

    195 + 951 = 786

    786 + 687 = 1473

    1473 + 3741 = 5214

    5214 + 4125 = 9339 Resulting palindrome

 

 In this particular case the palindrome 9339 appeared after 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome.

 

 Your task is to write a program that gives the resulting palindrome and the number of iterations (additions) to compute it. All tests data in this problem will have an answer, will be computable with less than 1000 iterations (additions), which will yield a palindrome that is not greater than 4,294,967,295.

 

 Input

                    The first line will have a number N (0<N<=100) with the number of test cases, the next N lines will each have a number P to compute its palindrome.

 

Output

          For each of the N numbers you will have to write a line with the minimum number of iterations (additions) to get to the palindrome and the resulting palindrome separated by one space.

 

Sample Input

3

195

265

750

 

Sample Output

4 9339

5 45254

3 6666

 

 


Author: rushikesh30
Date Added: 10-03-2010
Time Limit: 5 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

There is a typo in your

sharfah @ 31 Mar 2010 06:59 PM

There is a typo in your example. It should be "195 + 591 = 786", not "195 + 951 = 786".

@admin I am getting a runtime

mahendrakariya @ 18 Jun 2010 05:19 PM

@admin

I am getting a runtime error for my code in Python. Its working fine on my system. Can you push me a little bit so that I can solve this ques? My submission id is 270474

/* this code working fine

DevilsEye @ 21 Jun 2010 01:27 PM

  1. /* this code working fine on my machine but still it gives error everytime I tried to run in here...so is that error plz hep
    #include<stdio.h>
  2. unsigned long palin(unsigned long temp);
  3. int main()
  4. {
  5. unsigned long a,b,c;
  6. int itr=0;
  7. scanf("%lu",&a);
  8. while(1)
  9. {
  10. b=palin(a);
  11. c=a+b;
  12. itr++;
  13. if(c!=palin(c))
  14. {
  15. a=c;
  16. }
  17. else
  18. {
  19. printf("%d %lu",itr,c);
  20. break;
  21. }
  22. if(itr>1000)
  23. {
  24. break;
  25. }
  26. }
  27. return 0;
  28. }
  29. unsigned long palin(unsigned long temp)
  30. {
  31. unsigned long rev,temp1;
  32. rev=0;
  33. while(temp>0)
  34. {
  35. temp1=temp%10;
  36. temp=temp/10;
  37. rev=(rev*10)+temp1;
  38. }
  39. return(rev);
  40. }

Input : 196 Output 75

DevilsEye @ 21 Jun 2010 01:35 PM

Input :

196

Output

75 919969919

            My code

manisha_shetty @ 30 Jun 2010 09:47 PM

 

 

 

 

 

 

My code successfully compiles and runs on jdk 1.3 on my PC. but on the website it shows compilation error saying" compilation is successfull but main class is not found".the folowing is my code.Can someone please help.Are there any conventions for naming the main class.??  (thank you)

 

import java.io.*;
class palind
{
long reverse(long b)
{
long c=0;
while(b!=0)
{
c=c*10+b%10;
b/=10;
}
return(c);
}
}
class chef_palind
{
public static void main(String[] args)throws IOException
{
int i,j,n;
palind p=new palind();
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(b.readLine());
long a[]=new long[n+1];
for(i=1;i<=n;i++)
a[i]=Long.parseLong(b.readLine());
for(i=1;i<=n;i++)
{
j=0;
while(p.reverse(a[i])!=a[i])
{
j++;
a[i]+=p.reverse(a[i]);
}
System.out.println(j+" "+a[i]+"n");
}
}
}

Read the FAQ.

Innovator_C @ 30 Jun 2010 11:50 PM

Read the FAQ.

Thank you Ayush..that was so

manisha_shetty @ 1 Jul 2010 11:07 AM

Thank you Ayush..that was so stupid of me..!!

 

i kno its dumb but cn anyone

Prateek1990 @ 23 Jul 2010 12:52 AM

i kno its dumb but cn anyone pls tell me how exactly to take the input?
my programme is running successfully in borland c++;

but here even though i hv used the required syntax for c++(gcc++ 4.3.2) it says wrong answer :(

// Palindrome.cpp : Defines

Eshad19 @ 23 Aug 2010 02:17 PM

// Palindrome.cpp : Defines the entry point for the console application.
//

#include<iostream>
#include<stdio.h>
using namespace std;

struct pali
{
int a,count;
};
struct pali p[100];

class pm
{
public:
int rev(int i);
int rev_a(int j);
int add(int a, int b);
private:
int n1,n2;
};

int pm::add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}

int pm::rev(int i)
{
int temp=0,test = i,s_result = test;
p->count = 0;
thisplace:
n2 = s_result;
test = n2;
temp = 0;
while(test!=0)
{
temp*=10;
temp +=test%10;
test /=10;
}
n1=temp;
p->count += 1;
if(n1 != n2)
{
s_result = pm::add(n1,n2);
goto thisplace;
}
if(n1 == n2)
{
p->count--;
cout << p->count << " " ;
return n1;
}
else
cout <<"No match ";
return 0;
}

int main()
{
pm myPM;
int n;
cout << "How many line do you want : ";
cin >> n;
cout << "Enter numbers : ";
for(int i = 0; i<n; i++)
{
cin >> p[i].a;
}
int equal,equal2;
for(int i=0; i<n; i++)
{
equal2 = p[i].a;
cout <<myPM.rev(equal2) <<endl;
}
return 0;
}


@Devarsh : Input

manoharsingh23 @ 3 Nov 2010 10:01 PM

@Devarsh :

Input :

196

Output

44  588755786687557885

oh god...cant believe

vini89 @ 4 Dec 2010 07:39 AM

oh god...cant believe this...even missing out a "n" can get one a wrong answer here..

by "\n" i mean next line

vini89 @ 4 Dec 2010 07:41 AM

by "n" i mean next line character

Of course it can; if you

triplem @ 4 Dec 2010 09:18 AM

Of course it can; if you don't print any space between your answers, how could the judge know where one answer ends and the next one starts?

Should'nt this problem be in

rip13 @ 22 Jan 2011 04:43 PM

Should'nt this problem be in the easy section???

please check my program whats

asachin96 @ 15 Apr 2011 07:08 PM

please check my program whats wrong in it

  1. #include<stdio.h>
  2. main()
  3. {
  4. int n,count=0,b,temp;
  5. scanf("%d",&n);
  6. label:
  7. temp=n;
  8. b=0;
  9. while(n!=0)
  10. {
  11. b=b*10;
  12. b+=n%10;
  13. n/=10;
  14. }
  15. count++;
  16. if(b==temp)
  17. printf("%d/t%d",count-1,b);
  18. else
  19. {
  20. n=temp+b;
  21. goto label;
  22. }
  23. }

my python code runs perfectly

aveekmukh90 @ 17 May 2011 12:21 AM
my python code runs perfectly fine on my pc or at www.Ideone.com..but i get runtime error here. my code: cnt=input() #get the range for i in range(cnt): raw_input() #ignore the blank space a,c=input(),0 while(a!=int(str(a)[::-1])): #code a=a+int(str(a)[::-1]) c=c+1 print c,a any idea ne1?

umm.... this line: 195 + 951

pc1ang @ 29 Jun 2011 08:54 PM
umm.... this line: 195 + 951 = 786 ??

Hello Guys.The example is

pradeeps @ 8 Aug 2011 07:59 PM
Hello Guys.The example is wrong.don,t get confused

pretty easy problem this

promaster @ 12 Sep 2011 01:35 AM
pretty easy problem this

can someone please tell me

shoumo14 @ 12 Sep 2011 06:54 PM
can someone please tell me what is wrong in my code, it is running on my system using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace palindrome { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to the program of palindrome finder"); Console.WriteLine("Enter the number of test cases"); int i = Convert.ToInt32(Console.ReadLine()); int count = 0; for (int j = 1; j <= i; j++) { Console.Write("Enter the number for which you want to find the palindrome:"); int num = Convert.ToInt32(Console.ReadLine()); int num1=num; Program p = new Program(); int rev = 0; while ( count <= 1000) { rev = p.reverse(num1); if (rev == num1) break; num1 = num1 + rev; count++; } if (count <= 1000) { Console.WriteLine("the palindrome of the number is:{0} and the count is:{1}", rev, count); } count = 0; rev = num1 = 0; } Console.ReadLine(); } public int reverse(int n) { int sum = 0; int per = 0; while (n != 0) { per = n % 10; sum = (sum * 10) + per; n = n / 10; } return (sum); } } }

i'm geeting answers correct

adiki @ 20 Jan 2012 05:40 PM
i'm geeting answers correct in other compilers but overhere i'm getting runtime error for this probelm..can any one tell me what might the problem be ? #include int checkpalindrome(int a) { int q,r,m[10],i=0; q = a; while(1) { r = q%10; q = q/10; m[i] = r; if(q==0) break; i++; } q = 0; r = i; while(m[q]==m[i]) { if(q==i||(i-q)==1) return 1; q++; i--; } return 0; } int reverse(int a) { int q,r,m[10],i=0; q = a; while(1) { r = q%10; q = q/10; m[i] = r; //printf("r%dn",r); if(q==0) break; i++; } a=0;r=0; while(1) { a = a*10+m[r]; if(i==r) break; r++; } return a; } main() { int input,in1,in2,in3,i,j=0,k,m=0; //number of inputs scanf("%d",&input); //entering the first input for(i=0;i

wat therun time mean???

rohit290693 @ 27 Jan 2012 05:55 AM
wat therun time mean???

Ans of 2nd test case is 8888

raghuu1990 @ 12 Feb 2012 01:35 AM
Ans of 2nd test case is 8888

int power(int a,int

pintuguptajuit @ 14 Feb 2012 10:26 PM
int power(int a,int b) { if(a==1) return 1; int i; int sum=1; for(i=0;i

int power(int a,int

pintuguptajuit @ 14 Feb 2012 10:29 PM
int power(int a,int b) { if(a==1) return 1; int i; int sum=1; for(i=0;i

#include #include int

skyrocker @ 6 Mar 2012 11:32 AM
#include #include int rev(int n) { int a=0,b; while(n!=0) { b=n%10; n=n/10; a=a*10+b; } return a; } int pal(int x) { if(x==rev(x)) return 1; else return 0; } void main() { int t,i,*n,c,x; cin>>t; n=new int[t]; for(i=0;i>n[i]; } for(i=0;i

@admin:My program is showing

rahulkumar7887 @ 3 May 2012 04:51 PM
@admin:My program is showing following error what shall i do.?? anyone please help me out. Main.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com