C++ Palindromic Numbers with using arrays..Please somebody help

I am trying to get positive integer numbers as inputs and checks to see if those numbers are palindromic. Like that;

Enter a positive integer (0 to exit): <1228>
1228 is not a palindrome

Enter a positive integer (0 to exit): <15851>
15851 is a palindrome

Enter a positive integer (0 to exit): <hello>
*** error: inputs must be an integer

Enter a positive integer (0 to exit): <919>
919 is a palindrome

Enter a positive integer (0 to exit): <-99>
*** error: inputs must be greater than zero

Enter a positive integer (0 to exit): <4115>
4115 is not a palindrome

Enter a positive integer (0 to exit): <0>

I tried something but couldn't finish all codes. I had trouble with filling the bool function.. How can I fill in that Function, and also does that code logical??

These are my codes..

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
#include <iostream>
using namespace std;

bool is_a_palindrome(char[]){
.
.
.
.
.
.
}


int main(int, char**){

const int x=80;
const int END_VALUE=0;
long int nbr;
char checkPal[x];

do{

    cout<<"Enter a positive integer ("<<END_VALUE<<") to exit: ";
    cin>>nbr;
    
    char checkPal=nbr;
    
        if (cin.fail()) {
                cout << "*** error: inputs must be an integer" << endl;
                continue;  }
              
        else if(nbr<0){
                cout<<"*** error: inputs must be greater than zero"<<endl;
                continue;  }
        else if(is_a_palindrome(char checkPal[x])){
                cout<<nbr<<" is a Palindrome "<<endl;        
        }
        else
                cout<<nbr<<" is not a Palindrome"
    
    
}
while(nbr!=END_VALUE);


system("pause");
return EXIT_SUCCESS;
}


ALso there is one important note here
String objects cannot be used to determine if a number is a palindrome. In other words, this program must be implemented using an array of characters.
Last edited on
The first thing is to convert to number to a string, then compare the first to last, second to second last, until you have no more elements or you have a middle element. Hope it makes sense :)
Teacher doesnt allow to use string in that code.. and I dont have idea that how to fill boll function// :'(
It doesnt contain any idea about arrays.. I know how to write write codes without using arrays..but anyways thanks.. I am trying to find array functions..
Do you have to use char arrays? I could tell you how to do it with a number array, but I don't know how you could do it with chars.
I probably did wrong,, You could be right,, I was supposed to use number array..anyway, how can we do with number array? or which part do I have to switch?
You will want to put all the numbers from the original int into an int array. The hard part about that, for beginning programmers anyway, would be figuring out how to take the numbers out of the original one by one.

The first part is easy.
 
arrayName[position] = intName % 10;  //this will put the last number in the array 


The next part is the part that's the hardest.
 
intName = (intName - intName % 10) / 10;  //this will remove the last number from the int 


Repeat this until the int is zero.

Then all you need to do is reassemble the arrays into two ints (one will be forwards and one will be backwards) and compare them. If the ints don't compare, then the number isn't a palindrome. Make sure your array is a lot bigger than you'll ever need and won't be overloaded if someone enters a big number, or the program will crash.
Last edited on
Reassembly
 
intName = intName * 10 + arrayName[position];
so, you want me to cancel the char array and create a new int array like int nbr[]? I just learn the arrays , so I dont understand what you mean. You said compara them.. How can I do it?
You'll be comparing the ints, not the arrays. Your initiation is mostly correct but you need a number in the [].
Topic archived. No new replies allowed.