No output

Hi,

Im really really beginner. Im stuck with no outputs. How can i solve it? The code is for this question: A palindromic number reads the same both ways.
The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.


Heres my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <iostream>

using namespace std;

int main()
{  
	int a = 999;
	int b = 999;
	int n = a*b;
	int n1 = n%10;
	int n6 = n/100000;
	int n5 = (n%100000)/10000;
	int n4 = ((n%100000)%10000)/1000;
	int n3 = (((n%100000)%10000)%1000)/100;
	int n2 = ((((n%100000)%10000)%1000)%100)/10;
	
	while(b > 900)
	{
		while(a > 900)
	{
	if(n1 == n6 && n2 == n5 && n3 == n4)
	{
	  cout << n;
	}
	else
	{
	 a = a - 1;
	}
	}
		b = b - 1;
	}

	
   

	return 0;
}
Last edited on
Your program will never output a value because the condition used for the one output value, in line 22, will never be true. This is because n1 ends up becoming 1 and n6 becomes 9.98001. Since the condition is full of only the && operators which means if one value is false the whole value is false the block of code in the if statement never runs. And since that is the only place where you have an output statement the statement never runs. If you need help understanding control structures, such as while and if statements, try http://www.cplusplus.com/doc/tutorial/control/
what if i change my code as like this? (again no output)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;


	bool check(int n)
	{
			int w = 100;
		while(w<1000 && w>99)
		{
        if(n%w == 0)
	   return true;
		else
		{
			w = w + 1;
		}
		
	
		}
		return false;
	}

int main()
{  
	int n = 999999;
	string m = "n";

	
	
	while(n >= 100000)
	{
	if(check(n) && m.at(0)==m.at(5) && m.at(1)==m.at(4) && m.at(2)==m.at(3))
	{
     cout << m << endl;
	}
	else
	{
        n = n - 1;
	}
	}
	
	
	return 0;
}
On line 26 you are setting m equal to the string "n". Not the string 999999. This is because since the compiler is reading what's in the quotes as a string it won't consider that you are referencing the variable. So when you try to access m at any other point than 0 it won't work. If you want m to equal a string that is n use the function to_string(). Contained in header string. So line 26 would look more like string m = to_string(n);. Also I believe there's a much easier way to solve your problem. Try using for loops that multiply all 3 digit numbers together starting from 999 going down. If possible could you please use indents when nesting, it makes your code much easier to read.
i think this might help.......

#include<iostream>
#include<conio.h>

using namespace std;

class pallindrome{
public :

void largestpallindrome()
{
int a,b;
int z,num;
for(a=999;a>90;a--)
{
for(b=999;b>90;b--)
{
z = a*b;
num=z;

int sum=0,r;
while(z!=0)
{

r=z%10;
sum=sum*10+r;
z=z/10;
}
if(num==sum)
cout<<sum<<endl;

break;
}
}
}
};

int main()
{
pallindrome o;
o.largestpallindrome();
getch();
return 0;
}
Last edited on
Here I have taken a few minutes to make a simple version of the code. If this is homework, which it sounds like it is, make sure not to just use this code through copy and paste. Look through it and try to understand what makes it work. There is only one cout statement that no matter what executes. So you won't have the problem of no output anymore.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include <iostream>
using namespace std;

bool is_Palindrome(int palindrome);

int main()
{
	int num1, num2, pal = 0;
	//Loops through all three digit number multiples
	for (int i = 999; i >= 100; i--)
	{
		for (int j = i; j >= 100; j--)
		{
			//Checks if the multiple is a palindrome
			if (is_Palindrome(j * i))
			{
				//Checks if the palindrome is larger than all others
				if ((j * i) > pal)
				{
					pal = j * i;
					num1 = j;
					num2 = i;
				}
			}
		}
	}
	cout << "The largest palindrome created from three digit numbers is " << pal << "\nThese numbers are " << num1 << " and " << num2;
        return 0;
}

bool is_Palindrome(int palindrome)
{
	//Converts a number to a string
	string a = to_string(palindrome), c = " ";
	//Sets the first letter of the reversed string
	c[0] = a[a.length() - 1];
	int d = a.length() - 2;
	//Places the reversed string onto c
	for (int i = d; i >= 0; i--)
	{
		c += a[i];
	}
	//Checks if they are equal, if they are not, it is not a palindrome.
	if (a != c)
	{
		return false;
	}
	else
	{
		return true;
	}
}

If you have any questions about parts of the code, don't hesitate to ask.
Thank you all, im just using project euler. I've kinda solved it, thanks.

edit: I thought I got it but i didnot :( Yours is not working, too. My new code is that: I cant understand why its not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;

bool palindrome(int n)
{
	string m = to_string(n);
	string x = " ";
	int i = m.length() - 1;
	while(i >= 0)
	{
		x = x + m.substr(m.length() - 1,1);
	}

	if(m == x)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	int pal = 0;
	for(int i = 999; i >= 100; i--)
	{
		for(int j = i; j >= 100; j--)
		{
           if(palindrome(i * j))
		   {
			   if(i * j > pal)
			   {
				   pal = i * j;
			   }
		   }
		}
    }

	cout << pal;
	return 0;
}
Last edited on
A couple things on this program. First thing I see is that you have created an infinite loop.
1
2
3
4
5
int i = m.length() - 1;
while(i >= 0)
{
     x = x +m.substr(m.length() - 1, 1);
}

You never change the value of i so it always ends up being an infinite loop since i is always greater than 0. So you end up just continually appending the second to last letter of m. Even if this loop did somehow end x would never be equal to m since it is simply the same letter over and over. The substr() function only takes part of a string doesn't do anything to it. So when you add that part to x it never reverses the order.

About my code.
What are you using to test the code? Have you waited long enough for the code to run to completion?
Sorry, I forgot to edit my code, there is i-- there but again no output problem. I am using Visual Studio 2012
When you add the line i--; your code outputs 0. This is because in your while loop
1
2
3
4
5
while(i >= 0)
{
     x = x + m.substr(m.length() - 1, 1);
     i--;
}

You never change the letter that you are adding to x it is always the second to last letter. This makes it so m will never equal x. Due to this it will always return false. Since it is always returning false the variable pal is never changed and stays 0. That is why in my code I used a for loop and the variable i would be
whatever position that needed to be added to x, without much extra code.

Also I tried running your code with the added line i--; and I did get output however. I am also using Visual Studio and have gotten output for both yours and mine programs. Are you making sure to wait for the output? It might take some time depending on your system since there are about 900 numbers that the program has to go through and multiply to the same 900 numbers. So make sure to give your program time to go through and create the output.
Topic archived. No new replies allowed.