C Programming Examples
-
C Program to Check Palindrome Number Using Functions
Welcome to this tutorial on how to write a C program to check if a number is a palindrome using functions. Whether you are new to programming or looking to enhance skills, this blog post will guide you through the process by step.
Introduction
A palindrome number is number that remains the same when its digits are reversed. For example, 121 and 555 are palindrome numbers because they read the same from left to right or right to left. In this tutorial, we will write a C program to check if a given number is a palindrome or not, using functions.
Breaking Down the Problem
To solve this problem, we can break it down into smaller steps:
Accept a number from the user.
Reverse the number.
Compare the reversed number with the original number.
If they are the same, the number is a palindrome. Otherwise, it is not.
Let’s dive into each step in detail.
Accepting a Number from the User
In order to check if a number is a palindrome, we need to accept it from the user. We can use the
scanf()
function to read the number inputted by the user.#include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); return 0; }
We have declared a variable
number
of typeint
to store the user’s input. Theprintf()
function is used to prompt the user to enter a number, and thescanf()
function stores the input in thenumber
variable.Reversing the Number
To check if a number is a palindrome, we need to reverse it. We can create a separate function
reverseNumber()
to reverse the given number.#include <stdio.h> int reverseNumber(int num) { int reversedNumber = 0; while(num != 0) { reversedNumber = reversedNumber * 10 + (num % 10); num /= 10; } return reversedNumber; } int main() { int number; printf("Enter a number: "); scanf("%d", &number); int reversedNumber = reverseNumber(number); return 0; }
In the
reverseNumber()
function, we declare a variablereversedNumber
to store the reversed number. We use awhile
loop to iterate until the given number becomes 0. At each iteration, we multiply thereversedNumber
by 10 and add the remainder when dividing the original number by 10. This effectively builds the reversed number digit by digit. Finally, we return thereversedNumber
from the function.In the
main()
function, we call thereverseNumber()
function passing thenumber
as an argument. The reversed number is then stored in the variablereversedNumber
.Comparing the Reversed Number
Once we have the reversed number, we can compare it with the original number to check if they are the same. We can create another function called
isPalindrome()
to perform this comparison.#include <stdio.h> int reverseNumber(int num) { int reversedNumber = 0; while(num != 0) { reversedNumber = reversedNumber * 10 + (num % 10); num /= 10; } return reversedNumber; } int isPalindrome(int num) { int reversedNumber = reverseNumber(num); if(num == reversedNumber) { return 1; // The number is a palindrome. } else { return 0; // The number is not a palindrome. } } int main() { int number; printf("Enter a number: "); scanf("%d", &number); if(isPalindrome(number)) { printf("%d is a palindrome number.", number); } else { printf("%d is not a palindrome number.", number); } return 0; }
In the
isPalindrome()
function, we call thereverseNumber()
function to get the reversed number. We then compare the original number (num
) with the reversed number. If they are the same, we return 1, indicating that the number is a palindrome. Otherwise, we return 0 to indicate that the number is not a palindrome.Back in the
main()
function, we use anif
statement to check the return value of theisPalindrome()
function. If it returns 1, we print a message stating that the number is a palindrome. Otherwise, we print a message indicating that the number is not a palindrome.Putting It All Together
Now that we have discussed each step individually, let’s put everything together into a single program.
#include <stdio.h> int reverseNumber(int num) { int reversedNumber = 0; while(num != 0) { reversedNumber = reversedNumber * 10 + (num % 10); num /= 10; } return reversedNumber; } int isPalindrome(int num) { int reversedNumber = reverseNumber(num); if(num == reversedNumber) { return 1; // The number is a palindrome. } else { return 0; // The number is not a palindrome. } } int main() { int number; printf("Enter a number: "); scanf("%d", &number); if(isPalindrome(number)) { printf("%d is a palindrome number.", number); } else { printf("%d is not a palindrome number.", number); } return 0; }
Conclusion
In this tutorial, we have learned how to write a C program to check if a number is a palindrome using functions. We broke down the problem into smaller steps and discussed each step in detail. By following the code examples and explanations provided, you should now be able to write your own C program to check for palindrome numbers.
Remember, practicing is crucial to improving your programming skills. Try modifying the program or addressing additional requirements to further enhance your understanding. Keep exploring and pushing your boundaries to become a more proficient programmer.
If you want to delve deeper into the topic, you can explore concepts like recursion to solve the palindrome problem or try implementing the program in different programming languages. The possibilities are endless, and the more you experiment, the more you will learn.
Happy coding!