I made this program which had to calculate the Largest Palindrome which being the product of two 3-Digit Numbers.
My Program :

#include <iostream.h>
#include <conio.h>
int reverse(int num)
{
int rev=0,mod;
 while(num)
    {
      mod=num%10;
      num=num/10;
      rev=(rev*10)+mod;
    }
 return(rev);
}

void main()
{
textbackground(WHITE);
textcolor(BLACK);
clrscr();
long a,b,c,r=0,d;

for (a=999 ; a>=100 ; a--)
    {
      for (b=999 ; b>=100 ; b--)
        {
          c=a*b;
          d=c;
          r=reverse(c);
           if(r==d)
           break;
           cout <<"A : " <<a <<"\tB : " <<b <<"\tProduct : " <<c <<endl;
        }
      if(r==d)
      break;
    }
cout <<"Largest Palindrome is : " <<d;
getch();
}

It runs for 1-2 minutes and then suddenly stops without displayin any output.
A little help on whats going wrong?

Dani AI

Generated

Three quick points to check (they explain why the program either stops early or gives a wrong answer):

  • The inner break used when you first find a palindrome stops the whole search early; that finds a palindrome, not the largest. Track the maximum palindrome found and keep searching. This is the main logic bug was hinting at.
  • Non‑portable/non‑standard bits like conio.h, textbackground, void main() can hide platform behavior. Use standard headers (<iostream>) and int main() so the program behaves predictably, as suggested.
  • Use a type wide enough for the product (use long long or long) and make the palindrome check use the same type. On very old compilers int can overflow for 6‑digit products.

A simple, robust approach

  1. Loop a from 999 down to 100 and b from a down to 100 (avoid duplicate checks).
  2. Compute prod = (long long)a * b. If prod <= best you can break the inner loop (remaining b are smaller).
  3. Check palindrome (string reverse or numeric reverse using long long) and update best and the factor pair if larger.

Example implementation (modern, portable C++):

#include <iostream>
#include <string>
#include <algorithm>

bool is_palindrome(long long n){
    std::string s = std::to_string(n);
    std::string r = s;
    std::reverse(r.begin(), r.end());
    return s == r;
}

int main(){
    long long best = 0;
    int ba = 0, bb = 0;
    for(int a = 999; a >= 100; --a){
        for(int b = a; b >= 100; --b){
            long long p = 1LL * a * b;
            if(p <= best) break;               // optimization
            if(is_palindrome(p)){ best = p; ba = a; bb = b; }
        }
    }
    std::cout << "Largest palindrome: " << best << " = " << ba << " * " << bb << "\n";
    return 0;
}

Troubleshooting tips

  • Add debug prints (as suggested) if the program appears to hang; print loop indices occasionally.
  • Compile with a modern compiler (g++ -std=c++11) and remove conio.h calls.
  • If you still get an unexpected result, print sizeof(int)/sizeof(long long) and a few intermediate prod values to confirm no overflow.

With the fixes above the correct largest palindrome from two 3‑digit numbers is 906609 (993 * 913).

Recommended Answers

All 8 Replies

Output values to see what is happening while the program runs. This can also pinpoint where the error is and what variables have bad values.

Output stops at -
A : 146 B : 479 Product : 69934
Doesn't even check the palindrome condition, I guess :|

Its running perfectly for me.

Output :

A : 995 B : 583 Product 580085 Palindrome

And you need to change your program writing, using iostream.h void main() textbackground(WHITE); etc are not standard.

Your output is not inside the if clause. d variable is competely unnecessary. I do not understand why 33 and 34, which interrupt search prematurely (995 and 583 is not correct answer), you are not doing the maximum finding part.

So many errors with my program --
Thanks alot np and pytony.
I guess the main error is with the memory evaluation...like np said, I use quite extra commands. :P
Still the answer is not correct...will try and correct the program and post it as soon as possible ^
^

Ok here it is...made the necesseray adjustments. Still Not Correct :|

#include <iostream.h>
#include <conio.h>
int reverse(int num)
{
int rev=0,mod;
 while(num)
    {
      mod=num%10;
      num=num/10;
      rev=(rev*10)+mod;
    }
 return(rev);
}

void main()
{
clrscr();
long a,b,c,r=0,d,x=0;

for (a=999 ; a>=100 ; a--)
    {
      for (b=999 ; b>=100 ; b--)
    {
      c=a*b;
      d=c;
      r=reverse(c);
       if(r==d)
        {
         if(x<d)
         x=d;
        }

    }
    }
cout <<"Largest Palindrome is : " <<x;
getch();
}

Ok here it is...made the necesseray adjustments. Still Not Correct :|

you really need to explain what happens when asking for help. Based on this statement we can't tell if
1) you now have compile errors
2) you now have link errors
3) your program crashes
4) it surprisingly prints the words to "Jana Gana Mana"

Every post needs to be explicit. Detail what the problem is, and where you think the error is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.