1

I am trying to find all the palindrome numbers under a given limit number using a check function.

This is my code:

#include<iostream>
using namespace std;

int checkPalindrom(int);
int main(){
    int num,sum;
    int lim;

    cout << "Insert limit number: ";
    cin >>  lim;

    cout << "Palindrome numbers within the limit are: ";
    for(num>0;num<=lim;num++){
        sum=checkPalindrom(num);
        if(num==sum)
            cout << num << " ";
    }
    return 0;
}

int checkPalindrom(int num){
    int sum=0,r;
    if (num){
        r=num%10;
        sum=sum*10+r;
        checkPalindrom(num/10);
    }
    return sum;
}

The result here is palindrome numbers till 9, despite 2 digits < number

Thank you in advance for your answers!

tux3
  • 7,171
  • 6
  • 39
  • 51
DragosAC
  • 13
  • 3
  • Say more about the error and why u are not happy with the code. – gsamaras Jan 08 '15 at 22:48
  • Hi Samaras! If I insert for example 121 it will list palindromes only till 9, ignoring 11, 22, 33 .... and so on – DragosAC Jan 08 '15 at 22:49
  • `for(num>0;num<=lim;num++)` should be `for(num=0;num<=lim;num++)`, otherwise you are not initializing `num` – vsoftco Jan 08 '15 at 22:50
  • Since you seem to have something that works (and don't have a question), you should probably move this to [Code Review Stack Exchange](http://codereview.stackexchange.com/). They will offer suggestions to improve the code. – jww Jan 08 '15 at 22:50
  • I don't follow how your CheckPalindrome function actually tells you if a number is a palindrome or not. You have some logic errors in there for sure. Primarily, you call checkPalindrome recursively but do nothing with it's result. – Corey Ogburn Jan 08 '15 at 22:53
  • how is `checkPalindrom` supposed to work?? obviously its only effect is to return `num % 10` and all other stuff is completely ignored.. Go back to the drawing board on how to identify palindromes – stefan Jan 08 '15 at 22:53
  • The easier process is to convert the number to a string, then check the string for palindrome. Strings can be treated as an array where numbers are more difficult to treat as an array of digits. – Thomas Matthews Jan 08 '15 at 22:59

2 Answers2

1

Change this

for(num>0;num<=lim;num++)

to this

for(num=0;num<=lim;num++)

in order to initialize num.

then actually you need to make more changes. This should work:

#include<iostream>
using namespace std;

bool checkPalindrom(int);
int main() {
  int num;
  int lim;

  cout << "Insert limit number: ";
  cin >> lim;

  cout << "Palindrome numbers within the limit are: ";
  for (num = 0; num <= lim; num++) {
    if (checkPalindrom(num))
      cout << num << " ";
  }
  return 0;
}

bool checkPalindrom(int num) {
  int n = num;
  int dig, rev = 0;
  while (num > 0) {
    dig = num % 10;
    rev = rev * 10 + dig;
    num  /= 10;
  }
  return (n == rev);
}

The solution is based in this answer.

The main problem with your code, was that you were not even using a loop, but an if statement inside your function.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Tried that Samaras but the result is the same. For sure is a logic error as Corey Ogburn said – DragosAC Jan 08 '15 at 22:56
  • YeahI know, I am editing. :) – gsamaras Jan 08 '15 at 22:56
  • 1
    Suggestions for your implementation: Reduce the scope of `dig`, make the function return `bool` and maybe (that's style choice) write `num /= 10`. – stefan Jan 08 '15 at 23:05
  • 1
    Thank you, Samaras! Now I see my mistake. – DragosAC Jan 08 '15 at 23:07
  • Oh that's C++ @stefan, not C! Damn. I am editing, thanks. However I think the scope is OK the way is it too. You are welcomed and welcome to SO. Remember to be more descriptive about what goes wrong with your code. I have to admit, it was a fine first answer though, so +1. – gsamaras Jan 08 '15 at 23:10
  • @G.Samaras My personal rule is to give everything minimal scope. It gives a clear signal, that `dig` is not meant to be the same over several loop iterations or to be used before / after the loop. It's a maintenance thing, basically. – stefan Jan 08 '15 at 23:15
  • @stefan good point I will agree. However, for the scope of this question let's leave it as is. – gsamaras Jan 08 '15 at 23:16
  • 1
    @G.Samaras Sure. it's a good answer – stefan Jan 08 '15 at 23:17
0

In line thirteen there is a mistake, I think.

for (num = 0; num <= lim; num++)

Also in checkPalindrom function, it's better not to use recursion. It is much easier to write it like this

int checkPalindrom(int num) {
    int sum = 0, r;
    while (num){
        r = num % 10;
        sum = sum * 10 + r;
        num /= 10;
    }
    return sum;
}
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69