Palindrome function numbers

I'm stuck on trying to implement a function code for the palindrome number prototype I entered. I just don't know where to look. Something I've never done. Can someone give a hint or example? is it like an if statement. like say is it something like:
if(reverse = sumDigits){
return = true
}
else {
return = false}

The following is what I have to work with. All i have to really do is prototypes which is done and implement the function statements. Thank you for any assistance.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

const int NUM_VALS = 10;       //the maximum number of values to use

/********* Put the function prototypes below this line *********/
void sumDigits(int number){
    if(number < 10) cout << number;
    else{
        cout << number%10 + (number / 10);
    }
}
void reverse(int number){
    if(number < 10) cout << number;
    else{
        cout << number%10;
        reverse(number / 10);
    }
}
bool isPalindrome(int number);
bool isPrime(int number);






int main()
{
    
    int number,          //holds the random number that is manipulated and tested
    loopCnt;         //controls the loop
    
    //set the seed value for the random number generator
    //Note: a value of 1 will generate the same sequence of "random" numbers every
    //      time the program is executed
    
    srand(1);
    
    
    //Generate 10 random numbers to be manipulated and tested
    
    for( loopCnt = 1; loopCnt <= NUM_VALS; loopCnt++ )
    {
        //Get a random number
        number = rand();
        
        //Display the sum of adding up the digits in the random number, the reversed
        //random number, and whether or not the number is palindromic or a prime number
        
        cout << "The number is " << number << endl
        << "----------------------------------------" << endl
        << "Adding the digits result" << setw(16) << sumDigits( number ) << endl
        << "Reversing the digits result" << setw(13) << reverse(number) << endl
        << "Is the number a palindrome?" << setw(13) << (isPalindrome(number)? "Yes" : "No") << endl
        << "Is the number prime?" << setw(20) << (isPrime(number)? "Yes" : "No") << endl
        << endl << endl;
    }
    
    /** If the extra credit is being attempted, call the extra function below this line **/
    
    
    
    return 0;
}


/********* Code the functions below this line *********/
closed account (48T7M4Gy)
I wouldn't focus on what code to use. I would focus on getting a handle on a human procedure to perform the function.

Pseudocode is the key!. How would you check "palindromicity" if you did it manually? You could start off saying:

1. somehow get a string
2. assume it's not a palindrome to start off with
3. then, going through character by character
4. ... etc etc blah blah


Over to you ...

But if you just want to cut to the chase and learn zip then best advice is try google. :)
Thank you. That makes sense.
The following is what I came up with for the palindrome code where I have to call the reverse number. looking for the second set of eyes can someone correct me with what is wrong? Thank you for your input.


bool isPalindrome( int num, int reverse) // A recursive function to find out whether num is palindrome
// Base case (needed for recursion termination)

{
if (reverse(num))
return (num = (reverse) % 10);
else if (isPalindrome(num/10, reverse))
return false;

reverse /= 10;
return (num % 10 == (reverse) % 10);

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int reverse_digits( int n )
{
    int rev = 0 ;
    for( ; n != 0 ; n /= 10 ) rev = rev*10 + n%10 ;
    return rev ;
}

bool is_palindrome( int n ) { return n == reverse_digits(n) ; }

int main()
{
    for( int number : { 4, 0, -676, 677, 1234321, -1234421, -12344321, -11344321 } )
        std::cout << number << ' ' << std::boolalpha << is_palindrome(number) << '\n' ;
}

http://coliru.stacked-crooked.com/a/13537c4b15c4f684
Topic archived. No new replies allowed.