c++ Compiler giving wrong output.

connoisseur

Broken In
It's a program for finding the biggest palindrome possible by multiplying two 3-digit numbers.
I'm using Turbo C++ 3.0
However it's showing wrong outputs.
I'm 100% sure that the palindrome code is correct and I checked separately with "123" as input, even then it showed wrong output.
Please check the code for any errors and run it in your compiler.

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int a,b,r;    // a=num1   b=num2   r=remainder
	long int p,cp,np=0;   //p=product   cp=copy-of-product    np=new-product
	for(b=999;b>99;b--)      //outer-loop
	{
		for(a=999;a>99;a--)      //inner-loop
		{
			p=a*b;        //product
			cp=p;
			while(cp>0)    //palindrome code
			{
				r=cp%10;
				cp=cp/10;
				np=(np*10)+r;
			}
			if(np==p)      //if yes, break out of inner-loop
				break;
		}
		if(np==p)       //if yes, break out of outer-loop
			break;
	}
	cout<<"num1 = "<<a<<" num2 = "<<b<<" product = "<<p;
}
 
OP
C

connoisseur

Broken In
Can you please define the aim of the program more clearly?
Well, I think I already stated it above but I'll explain again.
Example: The largest palindrome made from the product of two 2-digit numbers is 9009 = 91*99.
Similarly, The program is for finding the two 3-digit numbers, whose multiplication gives the largest palindrome possible.
 

Neuron

Electronic.
There is one major mistake in your code.
The variable np is not being reset back to 0 after a reversal
 
OP
C

connoisseur

Broken In
There is one major mistake in your code.
The variable np is not being reset back to 0 after a reversal
Oops :oops:
Done.
But its still not providing the right answer.
Here's the code for checking the palindrome block separately, there was no need to setting np back to 0 here, but still wrong output:
Untitled.png
 

rijinpk1

Aspiring Novelist
Can this code be logically correct?
In the Inner loop 'a' starts with 999 and decreases to 100 while 'b' is constant at 999.
Suppose 999X121 yields a palindrome and your program prints it and exits.
If there exists a palindrome like 929X459, your program will never find it(the product of latter is higher than the first). Try better algorthm and it is time to change from turbo c.
 
This is what I wrote:

Code:
long unsigned int prod;
int num1, num2;

for(num1=999; num1 > 99; --num1)
     for(num2=999; num2 > 99; --num2)
          if(isPalindrome(num1*num2))
{
                 cout << num1*num2;
return0;
}

But I'm not getting the correct result (acc. to euler's Project).
 

rijinpk1

Aspiring Novelist
This is what I wrote:

Code:
long unsigned int prod;
int num1, num2;

for(num1=999; num1 > 99; --num1)
     for(num2=999; num2 > 99; --num2)
          if(isPalindrome(num1*num2))
{
                 cout << num1*num2;
return0;
}

But I'm not getting the correct result (acc. to euler's Project).

But once again, Can this code be logically correct?
In the Inner loop 'a' starts with 999 and decreases to 100 while 'b' is constant at 999.
Suppose 999X121 yields a palindrome and your program prints it and exits.
If there exists a palindrome like 929X459, your program will never find it(the product of latter is higher than the first).
Remember that op needs the largest palindrome.
 
Last edited:

skeletor

Chosen of the Omnissiah
Doesn't matter what IDE you use... just don't use Turbo C/C++ compiler. Old and dated. Outdated and ridiculous. Meaningless and should not be touched in 21st century.

I dunno why you are breaking out of loops when you don't have to. Let the loops run, you'll get your answer at the end....if your logic is right.

Check this out:

PHP:
#include <stdio.h>
#define LOWER 100
#define UPPER 999

int isPalindrome(long int);

int main()
	{
	long int i,j,num,large=0,a=0,b=0;
	for(i=LOWER;i<=UPPER;i++)
		{
		for(j=LOWER;j<=UPPER;j++)
			{
			num = i*j;
			if(isPalindrome(num))
				{
				if(num>large)
					{
					large = num;
					a = i;
					b = j;
					}
				}
			}
		}
	printf("a = %ld, b = %ld, large = %ld\n",a,b,large);
	return 0;
	}


int isPalindrome(long int x)
	{
	long int n,rev=0,dig;
	n = x;
	while(x!=0)
		{
		dig = x%10;
		rev = rev * 10 + dig;
		x = x/10;
		}
	if(n==rev)
		return 1;
	else
		return 0;
	}

Output:
a = 913, b = 993, large = 906609
 
Doesn't matter what IDE you use... just don't use Turbo C/C++ compiler. Old and dated. Outdated and ridiculous. Meaningless and should not be touched in 21st century.

I dunno why you are breaking out of loops when you don't have to. Let the loops run, you'll get your answer at the end....if your logic is right.

Check this out:

PHP:
#include <stdio.h>
#define LOWER 100
#define UPPER 999

int isPalindrome(long int);

int main()
    {
    long int i,j,num,large=0,a=0,b=0;
    for(i=LOWER;i<=UPPER;i++)
        {
        for(j=LOWER;j<=UPPER;j++)
            {
            num = i*j;
            if(isPalindrome(num))
                {
                if(num>large)
                    {
                    large = num;
                    a = i;
                    b = j;
                    }
                }
            }
        }
    printf("a = %ld, b = %ld, large = %ld\n",a,b,large);
    return 0;
    }


int isPalindrome(long int x)
    {
    long int n,rev=0,dig;
    n = x;
    while(x!=0)
        {
        dig = x%10;
        rev = rev * 10 + dig;
        x = x/10;
        }
    if(n==rev)
        return 1;
    else
        return 0;
    }

Output:
a = 913, b = 993, large = 906609

yeah, but going from 100x100 to 999x999 and displaying the latest palindrome product is the same as going from 999x999 to 100x100 and displaying the first palindrome product. that later approach (which I used) has lower complexity, but IDK why is it not giving correct result.
 

skeletor

Chosen of the Omnissiah
^^ may be your code has some other mistake.

btw there is one proper way of reducing the number of iterations. A palindrome having even digits is a multiple of 11. A 3 digit * 3 digit number will result in a number having 6 digits which is even number of digits. So, each palindrome will be a multiple of 11.
 

rijinpk1

Aspiring Novelist
Re: c Compiler giving wrong output.

yeah, but going from 100x100 to 999x999 and displaying the latest palindrome product is the same as going from 999x999 to 100x100 and displaying the first palindrome product. that later approach (which I used) has lower complexity, but IDK why is it not giving correct result.

it may not give correct result because the Inner loop 'a' starts with 999 and decreases to 100 while 'b' is constant at 999.
Suppose (b)999X(a)121 (by decrementing a )yields a palindrome and your program prints it and exits.
If there exists a palindrome like (b)929X(a)459 , your program will never find it if the first result is obtained. Yours prints only the first palindrome obatained no matter whether it is largest or not.
 

skeletor

Chosen of the Omnissiah
rijinpk1 is right.

If the outer loop and inner loop both go from 999 to 100 i.e. decrement, the first palindrome you get is 995 * 583 = 580085 and 995 * 517 = 514415. Iteration wise, these come before 993 * 913 = 906609.

Easiest way is by just letting the loops run and complete. Then print the largest result at the end.
 

Neuron

Electronic.
But once again, Can this code be logically correct?
In the Inner loop 'a' starts with 999 and decreases to 100 while 'b' is constant at 999.
Suppose 999X121 yields a palindrome and your program prints it and exits.
If there exists a palindrome like 929X459, your program will never find it(the product of latter is higher than the first).
Remember that op needs the largest palindrome.

True. It isn't correct.
 
Top Bottom