To determine whether a number is a palindrome, we need to check if it reads the same forwards and backwards. Here’s the algorithm, followed by the C program, and a detailed explanation of each step.
Algorithm to Check Whether a Number is Palindrome
Step 1: Start
Step 2: Input: Read an integer num from the user.
Step 3: Process:
- Initialize variables originalNum to store the original
number and reverseNum to store the reversed number.
- Set originalNum equal to num.
- Initialize reverseNum to 0.
- Use a loop to reverse the digits of num:
(i) Extract the last digit of num using num % 10.
(ii) Append this digit to reverseNum by multiplying
reverseNum by 10 and adding the extracted digit.
(iii) Remove the last digit from num using num / 10.
- After the loop, reverseNum will contain the reversed number of
num.
- Compare originalNum with reverseNum:
(i) If they are equal, num is a palindrome.
(ii) Otherwise, num is not a palindrome.
Step 4: Output: Display whether num is a palindrome or not.
Step 5: End
C Program to Check Whether a Number is Palindrome
#include <stdio.h>
int main() {
int num, originalNum, reverseNum = 0, remainder;
// Step 2: Read an integer from the user
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num; // Store the original number
// Step 3: Reverse the number and check for palindrome
while (num != 0) {
remainder = num % 10; // Extract the last digit
reverseNum = reverseNum * 10 + remainder; // Build the reversed number
num = num / 10; // Remove the last digit
}
// Step 4: Output whether the number is palindrome or not
if (originalNum == reverseNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
// Step 5: End
return 0;
}
Explanation of Each Step
Include Header:
#include <stdio.h>
- This line includes the standard input-output library.
Main Function:
int main() {
- The main function is the entry point of the C program.
Variable Declaration:
int num, originalNum, reverseNum = 0, remainder;
- Four integer variables are declared: num to store the user input, originalNum to store the original number, reverseNum to store the reversed number, and remainder to hold the remainder when dividing num by 10.
Reading Input:
printf("Enter an integer: ");
scanf("%d", &num);
- printf prompts the user to enter an integer.
- scanf reads the integer and stores it in the variable num.
Reversing the Number and Checking for Palindrome:
originalNum = num; // Store the original number
while (num != 0) {
remainder = num % 10; // Extract the last digit
reverseNum = reverseNum * 10 + remainder; // Build the reversed number
num = num / 10; // Remove the last digit
}
- originalNum is assigned the value of num to preserve the original input.
- In the while loop:
- remainder extracts the last digit of num.
- reverseNum is updated by appending remainder to its right (by multiplying reverseNum by 10 and adding remainder).
- num is updated to remove the last digit (num / 10).
Output:
if (originalNum == reverseNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
- The program checks if originalNum equals reverseNum.
- If they are equal, it prints that originalNum is a palindrome.
- Otherwise, it prints that that originalNum is not a palindrome.
End:
return 0;
- This line indicates successful completion of the program.
This program effectively determines whether a given integer num is a palindrome by reversing its digits and comparing it to the original number. If they match, the number is a palindrome; otherwise, it is not.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.