Advertisement
Mr_kindle

checkpalindrome.cpp

Nov 30th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | Source Code | 0 0
  1. /*
  2.     Name: checkpalindrome.cpp
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 30-11-22 15:14
  6.     Description: This code print all palindromes between a given range and also count the total number of
  7.     palindromes
  8. */
  9.  
  10.  
  11. #include<iostream>
  12. #include<math.h>
  13. using namespace std;
  14.  
  15. bool isPalindrome(int );
  16. int main()
  17. {
  18.     int max_range,i,count=0;
  19.     printf("Enter the maximum range (more than 10): ");
  20.     scanf("%d",&max_range);
  21.     printf("\n\n");
  22.    
  23.     /*Now checking each number one by one*/
  24.     for(i=10;i<=max_range;i++)
  25.         if(isPalindrome(i))//or if(ispalindrome == 1)
  26.             {
  27.                 printf("%d ",i);
  28.                 count++;
  29.             }
  30.     printf("\n\nTotal number of palindrome between 10 & %d is = %d\n\n",max_range,count);
  31.    
  32.  
  33. return 0;
  34. }//main
  35.  
  36. bool isPalindrome(int num)
  37.     {
  38.         int k,count=0;
  39.         int rem, sum = 0;
  40.         k = num;
  41.        
  42.        /*Counting total number of digits in the given number*/
  43.         do
  44.             {
  45.                 count++;
  46.                 k = k/10;
  47.             }
  48.             while(k);// while (num != 0)
  49.         /*K = 0 now hence reassign the value of num to k*/  
  50.             k = num;
  51.         /*Reversing the number*/
  52.         while(k)//or while(k!=0)
  53.             {
  54.                 rem = k%10;
  55.                 sum = sum + rem * pow(10, count - 1);  
  56.                 k /=10; //shortening the number by one digit
  57.                 count--;  
  58.             }
  59.            
  60.         if(num == sum)
  61.            return 1;
  62.         else
  63.            return 0;
  64.     }//end of ispalindrome
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement