Write C++ program to check palindrome number using recursion


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. // Function declarations
  6. int reverseNumber(int num);
  7. int isPalindrome(int num);
  8.  
  9. int main()
  10. {
  11. int num;
  12.  
  13. // Inputting any number from user
  14. cout<<"Enter any number: ";
  15. cin>>num;
  16.  
  17. if(isPalindrome(num) == 1)
  18. {
  19. cout<<num<<" is palindrome number";
  20. }
  21. else
  22. {
  23. cout<<num<<" is NOT palindrome number";
  24. }
  25.  
  26. return 0;
  27. }
  28.  
  29.  
  30. int isPalindrome(int num)
  31. {
  32.  
  33. if(num == reverseNumber(num))
  34. {
  35. return 1;
  36. }
  37.  
  38. return 0;
  39. }
  40.  
  41.  
  42. int reverseNumber(int num)
  43. {
  44. // Finding number of digits in num
  45. int digit = (int)log10(num);
  46.  
  47.  
  48. if(num == 0)
  49. return 0;
  50.  
  51. return ((num%10 * pow(10, digit)) + reverseNumber(num/10));
  52. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts