Advertisement
Mr_kindle

palindrome.c

Nov 30th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. /*
  2.     Name: palindrome.c
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 30-11-22 15:00
  6.     Description: This code checks whether a number is palindrome or not.
  7. */
  8.  
  9.  
  10.  
  11. #include<stdio.h>
  12. #include<math.h>
  13.  
  14. void isPalindrome(int );
  15. int main()
  16. {
  17.     int num;
  18.     printf("Enter the number: ");
  19.     scanf("%d",&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--;  
  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