Write a C program to check whether a number is a palindrome or not, In this C program example, we will see how to check whether a number is a palindrome or not by using for loop and while loop in the array.
What is a palindrome number?
Those number that remains same whenever we applied to reverse of number is known as a palindrome number, for example, 121 is a number, and in the reverse case, it remains same 121, then now 121 is a palindrome number and we will other examples input, which is given below.
Sample Input – 1
enter the numbers: 1235321
Sample output
the reverse of numbers: 1235321
it is a palindrome number.
Sample Input – 2
enter numbers: 14562
Sample output
the reverse of numbers: 26541
it is not a palindrome number.
Algorithm to check whether a number is palindrome or not
- Declare one array and four variables.
- Take the input for getting numbers from users.
- Then, take two for loop, one while loop, and the if-else statement.
- Print with a appropriate message in statements.
C program to check whether a number is palindrome or not
#include<stdio.h> void main (){ int i,a[10],n,r,sum; printf("enter numbers:"); for(i=0;i<1;i++){ scanf("%d",&a[i]); } for(i=0;i<1;i++){ n=a[i]; sum=0; while(n>0){ r=n%10; sum=sum*10+r; n=n/10; } printf("reverse of numbers :%d \n",sum); if(sum==a[i]){ printf("it is palidrome number\n"); } else{ printf("it is not a polidrome number"); } } }
Explanation of this C program
Step 1: Start.
Step 2: Create a header file and include the library in it.
Step 3: Create void main function.
Step 4: Declare one array a[1] and four variable (i,n,sum,r).
Step 5: Create input with help of scanf for taking numbers from the user, and create input in for loop.
Step 6: Create two for loop, first for input and in another for loop create while loop forgives the reverse logic (r=n%10, sum=sum*10+r, n=n/10) to check palindrome.
Step 7: Then, create if-else statement, in if statement give condition (sum==a[i]), to print it is a palindrome number.
Step 8: In else statement, to print it is not a palindrome number.
Step 9: Print appropriate messages with the help of printf.
Step 10: End.
The output of this C program
In this way, we learned how to write a C program to check whether a number is a palindrome or not