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.
#include <iostream>
usingnamespace 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;
}
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/
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.
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.
#include <string>
#include <iostream>
usingnamespace 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)
{
returnfalse;
}
else
{
returntrue;
}
}
If you have any questions about parts of the code, don't hesitate to ask.
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?
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.