Need Help!How to rewrite the Palindrome function recursively?

I don't have ideas to rewrite the Palindrome function recursively.
Can you guys help me?or tell ideas how can i write.
Thanks a lot.

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
// Check if a positive integer is a Palindrome
#include <iostream>
using namespace std;

bool isPalindrome(int number, int factor);

int main()
{
	int number; // a positive integer
	cout << "Enter a positive integer: ";
	cin >> number;

	// puts 10^(numDigits-1) (i.e., the smallest numDigits-digit positive integer) into factor
	int temp = number;
	int factor = 1; // power of ten
	while (temp > 9)
	{
		temp /= 10;
		factor *= 10;
	}

	// print whether the number is a palindrome
	if (isPalindrome(number, factor))
		cout << endl << number << " is a palindrome." << endl << endl;
	else
		cout << endl << number << " is not a palindrome." << endl << endl;

	system("pause");
}
bool isPalindrome(int number, int factor){
	int temp = number;
	int b = 0;
	while (temp != 0)
	{
		b = b * 10;
		b += temp % 10;
		temp /= 10;
	}
	return number == b;
}
Last edited on
A palindrome is a number such as 1234321. The function should take the largest digit, and check if it's the same as the highest digit. If not, your number isn't a palindrome. If yes, repeat for the inner number.

In this case, here are some examples:
1       => single digit, therefore yes
--------
12      => 1 != 2, therefore no
--------
121     => 1 == 1, therefore yes
 2      => single digit, therefore yes
--------
1234421 => 1 == 1, therefore yes
 23442  => 2 == 2, therefore yes
  344   => 3 != 4, therefore no

You may find it easier to program if you turn the number into a string (or just input a string in the first place).
Last edited on
I understand your meaning. Something like that?
1
2
3
4
5
6
7
8
9
10
bool isPalindrome(int number, int factor){
	int checkFirst,checkSecond;

	checkFirst = number % factor;
	number /= 10;
	checkSecond = number % factor;

	if (checkFirst != checkSecond)
		return false;
}

Actually, I don't how to code the recursive bool function of Palindrome number.
Can you tell more about how to code the recursive bool function?
Thanks!
I won't solve your problem for you, but something to think about: What exactly is a recursive function? What needs to happen for a function to be recursive?

Also, there are some functions in the <cmath> library which could be helpful. Take a look at this:
1
2
3
4
5
int num = 12345;
int len = static_cast<int>(std::log10(num)); // 4, cast to an integer
int div = static_cast<int>(std::pow(10, len)); // 10000
// num / div == 1 (cast to integer)
// num % 10  == 5 


You should be able to work it out from that.
I am following your meaning to code the function. What is the problems?Should i miss something?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool isPalindrome(int number, int factor){
	int checkFirst, checkSecond, temp;
	if (number / 10 > 0){
		checkFirst = number / factor;
		checkSecond = number % 10;
		if (checkFirst == checkSecond){
			temp = number%factor;
			number = temp;
			isPalindrome(number / 10, factor / 10);
		}
		else
		{
			return false;
		}
	}
	else
	{
		return true;
	}
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/177254/
Topic archived. No new replies allowed.