Last active
December 18, 2025 15:21
-
-
Save neps-in/228161b6ae4f8556abe45a9a4311fb09 to your computer and use it in GitHub Desktop.
This program checks whether a given integer is a palindrome.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| ============================================================================ | |
| Program Name : Palindrome Number Checker | |
| Filename : check-palindrome-v0.c | |
| Description : This program checks whether a given integer is a palindrome. | |
| A palindrome number reads the same forwards and backwards. | |
| Example: 121, 1331, 7. | |
| Approach : The number is reversed using arithmetic operations | |
| (modulus and division). The reversed value is then | |
| compared with the original number. | |
| Input : An integer entered by the user. | |
| Output : Displays whether the input number is a palindrome or not. | |
| Constraints : - Negative numbers are not considered palindromes. | |
| - Works for all non-negative integers within int range. | |
| Time Complexity: O(d), where d is the number of digits. | |
| Space Complexity: O(1) | |
| Author : Napoleon Arouldas S. | |
| Website : https://neps.in | |
| LinkedIn : https://www.linkedin.com/in/meetneps | |
| Created On : 12-06-2000 | |
| ============================================================================ | |
| */ | |
| #include <stdio.h> | |
| // Adding constants for easy readability. | |
| #define TRUE 1 | |
| #define FALSE 0 | |
| int isPalindrome(int n){ | |
| int i,r,rev,n_preserved,palindrome; | |
| // initialise the reversed number to zero. otherwise it | |
| // can contain random , unpredictable number | |
| rev = 0; | |
| //Preserve the n as it gets destroyed over the loop | |
| n_preserved = n; | |
| /* | |
| get the reminder and start | |
| building the reversed number | |
| */ | |
| while( n > 0 ){ | |
| r = n % 10; | |
| n = n / 10; | |
| rev = rev * 10 + r; | |
| } | |
| // if the original number and reversed number are same then its a palindrome | |
| // number. | |
| if( n_preserved == rev ){ | |
| palindrome = TRUE; | |
| } else { | |
| palindrome = FALSE; | |
| } | |
| return palindrome; | |
| } | |
| int main(){ | |
| int number; | |
| printf("Enter a number: "); | |
| scanf("%d", &number); | |
| if (isPalindrome(number)) { | |
| printf("%d is a palindrome\n", number); | |
| } else { | |
| printf("%d is not a palindrome\n", number); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment