Palindrome Number Checker
Get ready to explore the world of palindromes with our new blog post! We're going to learn about a cool C program that can tell us if a number reads the same forwards and backwards. It's like a word that's the same even if you read it in reverse! We'll show you step by step how the program works, and you'll see how easy it is to use. So, if you're curious about how computers can do these tricks, join us and have some coding fun!
C programming to check whether given number is palindrome or not.
#include <stdio.h>
int main() {
int number, originalNumber, remainder, reversedNumber = 0;
printf("Enter an integer: ");
scanf("%d", &number);
originalNumber = number;
while (number != 0) {
remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
if (originalNumber == reversedNumber) {
printf("%d is a palindrome.\n", originalNumber);
} else {
printf("%d is not a palindrome.\n", originalNumber);
}
return 0;
}
Test the Program
Explanation
-
Step 1:
The program starts by including the standard input/output library
stdio.h
. -
Step 2:
In the
main()
function, integer variablesnumber
,originalNumber
,remainder
, andreversedNumber
are declared. - Step 3: The program prompts the user to enter an integer.
-
Step 4:
The original value of
number
is stored inoriginalNumber
. -
Step 5:
A
while
loop is used to reverse the digits ofnumber
and store them inreversedNumber
. -
Step 6:
An
if
statement checks iforiginalNumber
is equal toreversedNumber
to determine if it's a palindrome.
If you have any doubts, Please let me know