Palindrome.loop.range

Hi,
Any advise about what is wrong with this code? why its looping forever,
and giving wrong answer?
programm loads with no error but outputs are not as expected .
I did write it. i hope logic is write but.. as i`m just starting to learn.
programm ware ment to give out all poligrams in range from 10000 to 999999
Sorry for my bad English.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{	
	int x, l, orig, pal=0;

	    for( x=100000; x <= 999999; x++ )	
	    {	
		orig=x; 
		 
	    	while(x!=0)	
			{			
            		l=x%10 ;
			pal=pal*10 + l; 
			x=x/10; 
		   	}	
			if(pal==orig)  
		    cout<<"palindroms: " ;	cout << pal;	 
		}
}
Last edited on
What do you mean by "wrong with code"?

* Does it compile without errors?
* Does it run without crashing?
* Does it give expected output?

Was this code written by you or given to you?

What is the program supposed to do?


I don't like the indentation. That, however, is style and thus somewhat debatable.


I can see something "very wrong", but you are probably trying to learn and you will learn better, if you can answer some questions first.
You're calculating with your count-variable in your for-Loop.
After every while Loop x = 0;
how can i fix that? if i write "return x" at the end. the x value always will be the same ?
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
#include <iostream>
using namespace std;

int main()
{	
	int x=0;
	int l=0;
	int orig=0;
	int pal=0;

	    for(int y=100000; y <= 999999; y++ )	
	    {	
		    orig=y; //Your value will be given to orig and x
		    x=y;
		    pal = 0;  // Don't Forget to set pal to Zero you're calculating with it
		 
	    	while(x!=0)	
			{			
                l=x%10 ;
			    pal=pal*10 + l; 
			    x=x/10; 
		   	}	
			if(pal==orig)
			{
		        cout << "palindroms: " ;	
		        cout << orig << endl;	 // endl for new Line
			}
	    }
}
Last edited on
Thank you. this works.
"Works" is overrated. The real question is: do you understand why?

Lets take your original code, but remove everything that does not affect the operation of the loops:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
  int x;
  for ( x = 100000; x <= 999999; x++ ) {
    // x is something
    while ( x != 0 ) {
      x = x / 10;
    }
    // x == 0
  }
}

It should be clear that the while loop decreases x to 0.

On start of first iteration of the for loop the x == 100000.
At the end of first iteration x == 0.
After iteration the loop increments x (from 0 to 1).
The loop can do new iteration, because 1 <= 999999.

On start of second iteration of the for loop the x == 1.
At the end of second iteration x == 0.
After iteration the loop increments x (from 0 to 1).
The loop can do new iteration, because 1 <= 999999.

... forever.


The problem is thus that the while loop affects the for loop.
Should it? No.

The solution, as said, is to make a copy that you can modify without changing the x:
1
2
3
4
5
6
7
8
9
int main()
{
  for ( int x = 100000; x <= 999999; x++ ) {
    int temp {x};
    while ( temp ) {
      temp /= 10;
    }
  }
}

I did had an idea that x stays 0 at the end, but I didn`t understand how to change it.. thank you for explanation.
Topic archived. No new replies allowed.