Here in this program, we will code to check whether an entered number is a palindrome number or not. let's discuss the Palindrome Program in C.
Palindrome Program in C
What is a Palindrome number?
A palindrome number is a special number because it remains the same when its digit gets reversed. For example, 16461 is a palindrome number if you reverse this number still, we get 16461. By default, all the 1digit numbers are palindrome numbers.
There are various syntaxes and algorithms to find a palindrome in C, and in this tutorial, we have discussed some of those.
Method 1:
Using while loop and basic mathematical operations
Method 2:
Using Recursion
Method 3:
Using string and for loop
Method 4:
Using string and halfway for loop
Method 1: Palindrome program in C with while loop and Basic Math
Statements we will use in this program
 While loop
 % (arithmetic modules)
 If…else statement
Logic

First, we will ask the user to enter a number

Then using a while loop we will try to reverse the entered number using % and other athematic operation

At last, we will compare the reversed number with the entered number

If the number match, we will print the number is a palindrome if not we print its not a palindrome number.
Palindrome Program in C
#include<stdio.h>
int main()
{
int num,x, y=0,rev;
printf("Enter a Number: ");
scanf("%d",&num);
rev= num;
while(rev!=0)
{
x= rev%10;
y=x+(y*10);
rev=rev/10;
}
if(num==y)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
}
Output:
Enter a Number: 14641 14641 is a palindrome number
Analysis
The time complexity of this approach is O(N) and space complexity is O(1).
Method 2: Palindrome program in C using recursion function
Statements we will use in this program

Recursion

% (arithmetic modules)

If…else statement
Logic

Ask the user to enter a number

Set the initial value of the reverse number to 0.

Call the function that reverses the number entered by the user.

In the function, set the base condition if the value of the number is 0, return the reverse.

If not, calculate the remainder of the number by dividing it by 10, this will give us the last digit of the number and pop out the last digit.

Multiply the current value of the reverse number and add the reminder to it.

Then Call the function recursivly, by passing the value of the number divided by 10 and the current reverse number.
Program in C
#include<stdio.h>
// Recursive function to get a reverse number
int reverseNum(int num, int rev){
if(num == 0)
return rev;
int remainder = num % 10;
rev = rev * 10 + remainder;
// call the function recursively
return reverseNum(num / 10, rev);
}
int main ()
{
int num, rev = 0;
printf("Enter a Number: ");
scanf("%d",&num);
// get the reverse of number
rev = reverseNum(num, rev);
// palindrome if num and reverse are equal
if (num == rev)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
}
Output
Enter a Number: 1234321
1234321 is a palindrome number
Analysis
The time complexity of this approach is the same as the while loop, which is O(N) and the space complexity is O(1).
But it is a recursive function, and by calling the function N times, it brings some additional space complexity of O(N), which means it is less efficient than the while loop approach.
Method 3: Palindrome program in C using String for loop
Statements we will use in this program
 C String
 For loop
 If statement
Logic

Accept the number from the user as a string.

Set two pointers, one point to the index 0 and the second point to the last index of the string.

Create a for loop that starts from 0 upto the last index of the string.

Inside the for loop check, if the pointer value of the first is not equal to the last pointer.

If they are not equal, set the flag to 1, print the number is not a palindrome and break out of the loop.
Program in C
#include<stdio.h>
#include <string.h>
int main ()
{
char num[20];
int len, ptr1, ptr2, flag=0;
printf("Enter the number: ");
//get the number as a string
gets(num);
//length of the number
len = strlen(num);
if(len<=1)
{
printf("%s is a palindrome", num);
}
else{
ptr2 = len1;
for(ptr1= 0; ptr1<len; ptr1++)
{
if( num[ptr1] != num[ptr2])
{
printf("%s is not palindrome", num);
flag=1;
break;
}
ptr2;
}
if(flag==0)
{
printf("%s is palindrome", num);
}
}
}
Output
Enter the number: 1235321
1235321 is palindrome
Analysis
The time and space complexity of this program is, O(N), and O(1)
Method 4: Palindrome program in C using String function half for loop
In the above program, we loop through the complete string, but to verify the palindrome, we can check if the first half equals the last half.
Statements we will use in this program.
 C String
 For loop
 If statement
Logic
 Accept the number from the user as a string.
 Set two pointers, one point to the index 0 and the second point to the last index of the string.
 Create a for loop that starts from 0 upto the half of the length of the string.
 Check if the value of the pointer matches or not.
Program in C
#include<stdio.h>
#include <string.h>
int main ()
{
char num[20];
int len, ptr1, ptr2, flag=0;
printf("Enter the number: ");
//get the number as a string
gets(num);
//length of the number
len = strlen(num);
if(len<=1)
{
printf("%s is a palindrome", num);
}
else{
ptr2 = len1;
for(ptr1= 0; ptr1<=len/2; ptr1++)
{
if( num[ptr1] != num[ptr2])
{
printf("%s is not palindrome", num);
flag=1;
break;
}
ptr2;
}
if(flag==0)
{
printf("%s is palindrome", num);
}
}
}
Output
Enter the number: 1221
1221 is palindrome
Conclusion
Checking a palindrome number in C is very easy. The only logic it tasks is reversing the number that we want to check for palindrome. And to reverse a number in C we can use the simple logic where we keep dividing the number by 10 until its quotient becomes 0, which can be achieved with this algorithm
while(rev!=0)
{
x= rev%10;
y=x+(y*10);
rev=rev/10;
}
People are also reading:
 WAP in C++ and Python to find the greatest number among the three numbers
 WAP to Find Cube of a Number using Functions
 WAP in C++ & Python to Insert an Element in an Array
 WAP to print given series:1 2 4 8 16 32 64 128
 WAP to Print the Following Triangle
 WAP to print the truth table for XY+Z
 WAP to find quotient and remainder of two numbers
 Most Asked Pattern Programs in C
Leave a Comment on this Post