Advertisement
Mr_kindle

palindrome.cpp

Nov 30th, 2022 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | Source Code | 0 0
  1. /*
  2.     Name: palindrome.cpp
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 30-11-22 14:59
  6.     Description: This code checks whether a number is palindrome or not.
  7. */
  8.  
  9.  
  10. #include<iostream>
  11. #include<math.h>
  12. using namespace std;
  13.  
  14. void isPalindrome(int );
  15. int main()
  16. {
  17.     int num;
  18.     cout << "Enter the number:\n";
  19.     cin >> num;
  20.     isPalindrome(num);
  21.  
  22. return 0;
  23. }//main
  24.  
  25. void isPalindrome(int num)
  26.     {
  27.         int k,count=0;
  28.         int rem, sum = 0;
  29.         k = num;
  30.        
  31.        /*Counting total number of digits in the given number*/
  32.         do
  33.             {
  34.                 count++;
  35.                 k = k/10;
  36.             }
  37.             while(k);// while (num != 0)
  38.         /*K = 0 now hence reassign the value of num to k*/  
  39.             k = num;
  40.         /*Reversing the number*/
  41.         while(k)//or while(k!=0)
  42.             {
  43.                 rem = k%10;
  44.                 sum = sum + rem * pow(10, count - 1);  
  45.                 k /=10; //shortening the number by one digit
  46.                 count--; //to decrease the power of 10 by 1 in the next loop
  47.             }
  48.            
  49.         if(num == sum)
  50.             printf("\n\nIt is a palindrome ");
  51.         else
  52.             printf("\n\nIt is not a palindrome: ");
  53.     }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement