Question

In: Computer Science

Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...

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.

Solutions

Expert Solution

#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.


Related Solutions

Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java 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...
Please write a pep/9 assembly code that checks if a word or number is a palindrome
Please write a pep/9 assembly code that checks if a word or number is a palindrome
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT