It is a program to check whether the number is a palindrome or not in the c language. We will make it using for loop, a while loop, and functions.
Palindrome program in c using for loop
In this program, we will take the input number from the user. We will take another variable which will store the reverse number.
We will take the modulus of the current number by 10, which gives the unit digit of the current number. We will multiply the "reversed number" by ten and add the unit digit. At last, we will divide the current number by 10.
We will do this process till the current number is greater than zero.
#include<stdio.h> int main() { int i, n, remainder, rev=0; printf("\n Enter a number:"); scanf("%d",&n); for(i=n;i>0;i=i/10) { remainder=i%10;
rev=rev*10+remainder;
} if(rev==n) printf("\n It is a palindrome"); else printf("\n It is not a palindrome"); return 0; }
Palindrome program in c using while loop
#include<stdio.h> int main() { int i, n, r, s = 0; printf("\n Enter a number:"); scanf("%d", &n); i = n; while (i > 0) { r = i % 10; s = (s * 10) + r; i = i / 10; } if (s == n) printf("\n It is a palindrome"); else printf("\n It is not a palindrome"); return 0; }
Palindrome program in c using functions
#include<stdio.h> int palindrome(int a); int main() { int b, c; printf("\n Enter a number:"); scanf("%d", &b); c = b; c = palindrome(c); if (b == c) printf("%d is a palindrome", b); else printf("%d is not a palindrome", b); return 0; } int palindrome(int a) { int i, r, s = 0; i = a; while (i > 0) { r = i % 10; s = (s * 10) + r; i = i / 10; } return s; }
Q. How do You Check if a number is a palindrome?
To check palindrome, we have to check its reverse should also equal to it.
For example:7227 is a palindrome as its reverse is also 7227.
Q. What is the palindrome program in c?
To make a palindrome program, we have to compare its reverse with the number entered by the user, and if its reverse is equal to the original number (number entered by the user), then it is a palindrome.
Q. How do you write a palindrome program?
First, we input a number by the user, then reverse it. At last, we compare the number entered by the user with its reverse if they are identical. We print the number entered by the user as a palindrome & if they are not identical, then we print the number entered by the user as not a palindrome.
Related Posts:
C program to find the greatest number among three numbers
Odd and even number program in c
C program to find the greatest number among three numbers
Odd and even number program in c
About this blog post
This program is brought to you by Coding Wallah. If you like this program or if this code works on your laptop/PC, comment below and share your experience.
Thanks for reading this blog post. I hope you enjoy this program as much as I enjoy offering them to you. If you have any questions or queries about this program, please do not hesitate to comment below or contact me.