to find the largest palindrome number 2-digit

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3364 Views - Last Post: 16 August 2012 - 01:44 PM Rate Topic: ****- 1 Votes

#1 Elkanah modi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

to find the largest palindrome number 2-digit

Posted 15 August 2012 - 11:32 AM

guys i was solving a problem of finding greatest 2-digit palindromic number its working for low number,
problem is that it will print till 626, not above this..can anyone tell me the reason

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    unsigned long long int c = 0,i,j,n,s,a,p;
    for (i = 26, j = 26; i <= 99, j < 99; i++,j++) {
        n = i * j;
        p = n;
        s = 0;
        while (n > 0) {
                a = n % 10;
                s = s * 10 + a;
                n = n /10;
          }
          if (p == s) {
             cout <<p << endl;
             }
    } 
    getch();
}


Is This A Good Question/Topic? 0
  • +

Replies To: to find the largest palindrome number 2-digit

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 11:44 AM

Because it can't find any more.

Put a cout << "Done." << endl; between lines 19 and 20. You'll see that it exits the loop because it couldn't find any more.

And this makes sense since the next palindromic square is 10201 according to this: http://oeis.org/A002779

sqrt(10201) == 101, and your loop only goes up to 99.

This post has been edited by Skydiver: 15 August 2012 - 11:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,042
  • Joined: 21-June 11

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 11:47 AM

This is unrelated to the problem, but:

for (i = 26, j = 26; i <= 99, j < 99; i++,j++)


Using the comma-operator in the condition part of a for-loop is almost always a mistake. Note that i <= 99, j < 99 is equivalent to just j < 99. The comma-operator only makes sense if the expression before the comma has a side-effect.

It doesn't really matter in this case though because j < 99 will always be false before i <= 99 anyway.

This post has been edited by sepp2k: 15 August 2012 - 11:49 AM

Was This Post Helpful? 1
  • +
  • -

#4 Elkanah modi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 12:09 PM

View PostSkydiver, on 15 August 2012 - 11:44 AM, said:

Because it can't find any more.

Put a cout << "Done." << endl; between lines 19 and 20. You'll see that it exits the loop because it couldn't find any more.

And this makes sense since the next palindromic square is 10201 according to this: http://oeis.org/A002779

sqrt(10201) == 101, and your loop only goes up to 99.

if though after changing the value of 99 t0 103 its not giving me the correct answer, can you tell me once again
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 12:14 PM

I changed the loop conditions to end at 103 and it works for me. :lol: Be sure to read sepp2k's post #4 above carefully.

Show us your new code where you changed the limit to 103.
Was This Post Helpful? 0
  • +
  • -

#6 Elkanah modi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 12:42 PM

View PostElkanah modi, on 15 August 2012 - 12:09 PM, said:

View PostSkydiver, on 15 August 2012 - 11:44 AM, said:

Because it can't find any more.

Put a cout << "Done." << endl; between lines 19 and 20. You'll see that it exits the loop because it couldn't find any more.

And this makes sense since the next palindromic square is 10201 according to this: http://oeis.org/A002779

sqrt(10201) == 101, and your loop only goes up to 99.

if though after changing the value of 99 t0 103 its not giving me the correct answer, can you tell me once again ..it should had print 9009 for (91 * 99)..but not working for this also...

Was This Post Helpful? 0
  • +
  • -

#7 Elkanah modi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 12:51 PM

View PostSkydiver, on 15 August 2012 - 12:14 PM, said:

I changed the loop conditions to end at 103 and it works for me. :lol: Be sure to read sepp2k's post #4 above carefully.

Show us your new code where you changed the limit to 103.

sorry...yep its work,but my main idea was to get 9009(91 * 99) which is the largest palindrome number of multiplication
of 2-digit...but its not working...any new reason....
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,042
  • Joined: 21-June 11

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 01:11 PM

View PostElkanah modi, on 15 August 2012 - 09:51 PM, said:

my main idea was to get 9009(91 * 99) which is the largest palindrome number of multiplication
of 2-digit


That's not what your code does though. You're always incrementing i and j at the same time, so given that they start at the same value, i will always be equal to j in your code (so you'll never get something like i=91 and j=99).

If you want to check each combination of i and j in a given range, you'll need to use two nested loops.
Was This Post Helpful? 1
  • +
  • -

#9 Elkanah modi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 01:11 PM

for 3- digit its giving me the answer,but the problem is that its not printing the largest palindrome in last,as it should be...not printing in increasing manner..

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    long int i,j,n,s,a,p,flag;
    for (i = 100; i < 1000; i++) {
        for (j = 100; j < 1000; j++) {
        n = i * j;
        p = n;
        s = 0;
        while (n > 0) {
                a = n % 10;
                s = s * 10 + a;
                n = n /10;
          }
          if (p == s) {
            flag = p;
            //cout << flag << endl;
            }
            
        }
        cout << flag << endl;
    } 
    getch();
}


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 01:40 PM

Why would you expect it to print the numbers in increasing order if you've done nothing special to make the code print it out that way?

If you look at a simple multiplication table, each time you finish a row and start a new row the products are more than the values on previous row of the corresponding column. Building a multiplication table is essentially what you are doing with lines 7-9.

If you want the results sorted, you'll have to either compute the results and sort them afterwards, or change the order you are computing your products so that each succeeding product is higher than the next.

I suck at sequences so I would probably just do the sorting, but some people who are more mathematically minded may know of a way to manipulate your i and j values to keep the product increasing.

This post has been edited by Skydiver: 15 August 2012 - 01:50 PM

Was This Post Helpful? 2
  • +
  • -

#11 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 3,042
  • Joined: 21-June 11

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 01:47 PM

View PostSkydiver, on 15 August 2012 - 10:40 PM, said:

If you want the results sorted, you'll have to either compute the results and sort them afterwards, or change the order you are computing your products so that each succeeding product is higher than the next.


And if all you want to know in the end is the largest palindrome, you don't need to sort at all, but instead simply keep track of which one is the largest palindrome you've seen up to now.
Was This Post Helpful? 2
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 01:48 PM

And the other thing is that lines 17-20 will just grab the palindrome. It doesn't check to see if it already has found a palindrome and it is bigger than the next one it is trying to grab.
Was This Post Helpful? 1
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3169
  • View blog
  • Posts: 9,595
  • Joined: 05-May 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 02:10 PM

Out of curiosity... the number 676 is palindromic, but it is not a 2 digit number. How can you class it as the largest 2 digit palindromic number? I think the largest 2 digit palindromic number is 99.

When I was running your program it (and with some tweaks to find the largest), it was finding 906609. Again it is palindromic, but it is not a 3 digit number. As I see it, it contains 6 digits. I think the largest 3 digit palindromic number is 999.

This post has been edited by Skydiver: 15 August 2012 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 Elkanah modi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 02:36 PM

View PostSkydiver, on 15 August 2012 - 02:10 PM, said:

Out of curiosity... the number 676 is palindromic, but it is not a 2 digit number. How can you class it as the largest 2 digit palindromic number? I think the largest 2 digit palindromic number is 99.

When I was running your program it (and with some tweaks to find the largest), it was finding 906609. Again it is palindromic, but it is not a 3 digit number. As I see it, it contains 6 digits. I think the largest 3 digit palindromic number is 999.

sorry for not explaining you the detail..actually i want to print the largest palindrome which is a multiplication of 3-digit number...i.e.906609....not the largest 3-digit palindrome..
Was This Post Helpful? 0
  • +
  • -

#15 Elkanah modi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-August 12

Re: to find the largest palindrome number 2-digit

Posted 15 August 2012 - 02:47 PM

yep,, thanks u both i have got the correct answer now, my new code for finding largest palindrome of 3-digit multiplication :
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    long int i,j,max = 0,n,s,a,p,flag;
    for (i = 100; i < 1000; i++) {
        for (j = 100; j < 1000; j++) {
        n = i * j;
        p = n;
        s = 0;
        while (n > 0) {
                a = n % 10;
                s = s * 10 + a;
                n = n /10;
          }
          if (p == s) {
            flag = p;
            if (max < flag)
               max = flag;
            }   
        }
    } 
    cout << max;
    cout << "done" ;
    getch();
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2