In: Computer Science
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you do in each part of the code. You need to run your code for the following test cases and show the result:
1) 0 (output: yes)
2) 1234554321 (output: yes)
3) 123454321 (output: yes)
4) 1221 (output: yes)
5) 1234 (output: no)
6) 7676 (output: no)
7) -121 (output: yes)
8) -456 (output: no)
What is the time complexity of your algorithm? Cleary justify your answer.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int isPalRecursive(int number, int rev)
{
if (number == 0)
return rev;
rev = (rev * 10) + (number % 10);
return isPalRecursive(number / 10, rev);
}
bool isPalindrome(int palin)
{
if (palin == isPalRecursive(palin, 0))
return true;
return false;
}
int main() {
if(isPalindrome(1234554321))
cout<<"1234554321 is palindrome\n";
else
cout<<"1234554321 is NOT a palindrome\n";
if(isPalindrome(7676))
cout<<"7676 is palindrome\n";
else
cout<<"7676 is NOT a palindrome\n";
if(isPalindrome(-121))
cout<<"-121 is palindrome\n";
else
cout<<"-121 is NOT a palindrome\n";
}
=====================================
SEE OUTPUT/ SEE IMAGE for more Output
Thanks, PLEASE COMMENT if there is any concern.