Introduction
A palindrome number is a number that remains the same when its digits are reversed. For example, 121 and 1331 are palindromes. This guide will show you how to write a C program to check whether a given number is a palindrome.
Problem Statement
Create a C program that:
- Takes an integer input from the user.
- Checks whether the number is a palindrome.
- Displays the result.
Example:
- Input:
121 - Output:
121 is a palindrome
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>to include the standard input-output library, which is necessary for usingprintfandscanffunctions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the original number, the reversed number, and the remainder.
- Input the Number: Use
scanfto take input from the user for the number. - Reverse the Number: Use a loop to reverse the digits of the number.
- Check for Palindrome: Compare the reversed number with the original number.
- Display the Result: Use
printfto display whether the number is a palindrome.
C Program
#include <stdio.h>
/**
* C Program to Check Palindrome Number
* Author: https://www.javaguides.net/
*/
int main() {
// Step 1: Declare variables to hold the original number, reversed number, and remainder
int num, originalNum, reverse = 0, remainder;
// Step 2: Prompt the user to enter a number
printf("Enter an integer: ");
scanf("%d", &num);
// Step 3: Store the original number
originalNum = num;
// Step 4: Reverse the number using a loop
while (num != 0) {
remainder = num % 10; // Get the last digit
reverse = reverse * 10 + remainder; // Append the last digit to the reversed number
num = num / 10; // Remove the last digit from the original number
}
// Step 5: Check if the original number is equal to the reversed number
if (originalNum == reverse) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
return 0; // Step 6: Return 0 to indicate successful execution
}
Explanation
Step 1: Declare Variables
- The variable
numis declared to store the original number entered by the user.originalNumis used to store the original number for comparison later.reverseis initialized to 0 and will hold the reversed number.remainderis used to store the last digit of the number during each iteration of the loop.
Step 2: Input the Number
- The program prompts the user to enter an integer using
printf. Thescanffunction then reads the input and stores it in the variablenum.
Step 3: Store the Original Number
- The original number is stored in the variable
originalNumso that it can be compared later with the reversed number.
Step 4: Reverse the Number Using a Loop
- The program uses a
whileloop to reverse the digits of the number:- Step 4.1: The remainder when
numis divided by 10 gives the last digit ofnum. - Step 4.2: This last digit is then added to the
reversevariable after multiplyingreverseby 10 to shift its digits to the left. - Step 4.3: The original number
numis divided by 10 to remove its last digit. This process repeats untilnumbecomes 0.
- Step 4.1: The remainder when
Step 5: Check if the Number is a Palindrome
- The program compares the original number
originalNumwith the reversed numberreverse. If they are equal, the number is a palindrome; otherwise, it is not.
Step 6: Display the Result
- The program displays whether the number is a palindrome using the
printffunction.
Step 7: Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example 1:
Enter an integer: 121
121 is a palindrome.
Example 2:
Enter an integer: 123
123 is not a palindrome.
Example 3:
Enter an integer: 1331
1331 is a palindrome.
Conclusion
This C program demonstrates how to check whether a given number is a palindrome. It covers basic concepts such as loops, arithmetic operations, and conditional statements, making it a useful example for beginners learning C programming.