# c++ Compiler giving wrong output.



## connoisseur (Apr 30, 2013)

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.


```
#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;
}
```


----------



## harshilsharma63 (Apr 30, 2013)

Can you please define the aim of the program more clearly?


----------



## connoisseur (Apr 30, 2013)

harshilsharma63 said:


> 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 (Apr 30, 2013)

There is one major mistake in your code.


Spoiler



The variable np is not being reset back to 0 after a reversal


----------



## connoisseur (Apr 30, 2013)

Neuron said:


> There is one major mistake in your code.
> 
> 
> Spoiler
> ...


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:


----------



## Neuron (May 1, 2013)

connoisseur said:


> 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:
> View attachment 10243



Looking at that screenshot, you haven't initialized np to zero there.


----------



## connoisseur (May 1, 2013)

So, apparently programming is not my cup of tea


----------



## Neuron (May 1, 2013)

Or may be you should drink a cup of tea and get some concentration going on.


----------



## harshilsharma63 (May 1, 2013)

You should first stop using turbo c or nothing will be your cup of tea.


----------



## Shah (May 1, 2013)

harshilsharma63 said:


> You should first stop using turbo c or nothing will be your cup of tea.
> 
> You should first stop using turbo c or nothing will be your cup of tea.



+100000 to this. 

@OP: Use CodeBlocks or Netbeans.


----------



## harshilsharma63 (May 1, 2013)

I prefer net beans over code blocks.


----------



## rijinpk1 (May 4, 2013)

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.


----------



## harshilsharma63 (May 4, 2013)

This is what I wrote:


```
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 (May 4, 2013)

harshilsharma63 said:


> This is what I wrote:
> 
> 
> ```
> ...



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.


----------



## ico (May 4, 2013)

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:


```
#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


----------



## harshilsharma63 (May 4, 2013)

ico said:


> 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.
> 
> ...



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.


----------



## ico (May 4, 2013)

^^ 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 (May 4, 2013)

*Re: c   Compiler giving wrong output.*



harshilsharma63 said:


> 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.


----------



## ico (May 4, 2013)

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 (May 4, 2013)

rijinpk1 said:


> 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.


----------

