C Program to Check Whether a Given Number is Palindrome or Not दी गई संख्या Palindrome है या नहीं, जाँचने का C प्रोग्राम
A palindrome number is a number that remains the same when its digits are reversed. For example, 121, madam is a word palindrome, and 1331 is a palindrome number.
Palindrome Number वह संख्या होती है जो उल्टा लिखने पर भी वही रहती है। उदाहरण के लिए, 121 और 1331 Palindrome Numbers हैं।
C Program
#include <stdio.h>
int main(void) {
int n, q, r, rev = 0;
printf("Enter an integer number:\n");
if (scanf("%d", &n) != 1) {
printf("Invalid input! Please enter an integer.\n");
return 1;
}
if (n < 0) {
printf("Please enter a non-negative integer.\n");
return 1;
}
q = n;
while (q != 0) {
r = q % 10;
q = q / 10;
rev = rev * 10 + r;
}
if (n == rev) {
printf("%d is a palindrome number.\n", n);
} else {
printf("%d is not a palindrome number.\n", n);
}
return 0;
}Sample Output 1
Enter an integer number:
121
121 is a palindrome number.Sample Output 2
Enter an integer number:
1234
1234 is not a palindrome number.Sample Output 3
Enter an integer number:
1331
1331 is a palindrome number.Explanation
Let us understand the program with the number 121.
The program first stores the original number in variable q. It then reverses the number using a while loop.
The modulus operator (%) extracts the last digit, while integer division (/) removes the last digit. The reversed number is calculated using:
[ rev = rev * 10 + r ]
| Step | q | Extracted digit (r) | Reversed number (rev) |
|---|---|---|---|
| 1 | 121 | 1 | 1 |
| 2 | 12 | 2 | 12 |
| 3 | 1 | 1 | 121 |
The reversed number is 121. Since the original number and reversed number are equal, 121 is a Palindrome Number.
प्रोग्राम सबसे पहले मूल संख्या को q वेरिएबल में स्टोर करता है। इसके बाद while लूप की सहायता से संख्या को Reverse किया जाता है।
Modulus Operator (%) अंतिम अंक प्राप्त करता है तथा Integer Division (/) अंतिम अंक को हटा देता है। Reversed Number निकालने के लिए निम्न Formula का उपयोग किया जाता है:
[
rev = rev \times 10 + r
]
अंत में मूल संख्या n और Reversed Number rev की तुलना की जाती है। यदि दोनों बराबर हैं, तो संख्या Palindrome होती है, अन्यथा नहीं।
Modern C Compatibility Note
This program uses standard C and works with modern compilers such as GCC, Clang, and Visual Studio C. It does not require conio.h, clrscr(), or getch().
This program demonstrates how to check a Palindrome Number using a while loop, modulus operator (%), and conditional statements.
यह प्रोग्राम while लूप, Modulus Operator (%) और Conditional Statements की सहायता से Palindrome Number की जाँच करना सिखाता है।
Comments
Post a Comment