C Program to Check Whether a Number is Palindrome or Not

«
»
Problem Description

Write a C Program to check whether a given number is Palindrome or not.

What is Palindrome Number in C?

A number is said to be a palindrome number if it reads the same forward and backward i.e., on reversing the digits of the number we get the same number.

Example: 121, 343.

Problem Solution

1. Enter the number.
2. Reverse the number.
3. Check if the reversed number is equal to the original number or not.
4. If they are equal, print the number is a Palindrome number else print, the number is not a palindrome number.

There are several ways to check whether a number is Palindrome or not in C. Let’s look at all different approaches to program this function:

Palindrome Number Program in C

String Palindrome Program in C

Note: Join free Sanfoundry classes at Telegram or Youtube

Method 1: Palindrome Program in C using While Loop

In this approach, we check for Palindrome number using while loop.

Program/Source Code

Here is the source code of the C program to check whether a number is palindrome or not using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to Check Number is Palindrome or Not using While Loop.
*/

#include <stdio.h>
int main()
{
int n, rev = 0;
printf("Enter the number: ");
scanf("%d", &n);

int num= n;  //To store the original number in the variable num

//Reverse the number and store it in variable rev
while (n > 0)
{
rev = rev * 10 + n % 10;
n = n / 10;
}

// check if original number is same as reversed number or not
if (num == rev)
printf("%d is a palindrome number.", num);
else
printf("%d is not a palindrome number.", num);

return 0;
}```
Program Explanation

1. Take the number, n as input from the user.
2. Initialize the variable num and store the value of n in it and also initialize rev=0.
3. Run a while loop till n is greater than 0.
4. Inside the while loop reverse the number n by using the formula rev=rev*10 + n%10 and in each iteration recalculate the value of n as n/10.
5. After reversing the number and storing it in the variable rev check if num is equal to rev or not.
6. If num is equal to rev print the number as palindrome number.
7. Else, print the number as not a palindrome number.

Example:

• Consider the number, n entered by the user is 121.
• Assign the number 121 in another variable num. In the while loop check if n>0 or not.
• Since n=121 condition becomes true and enter the loop. Inside the loop calculate rev as rev=rev*10 + n%10 i.e., rev= 0*10 + 121%10 = 0 + 1 = 1.
• Now, calculate the value of n as n=n/10 = 121/10 = 12. Repeat this process till n is greater than 0.
• In the next iteration rev = 1*10+12%10 = 10+2 = 12 and n = 12/10 = 1. Again, n is greater than 0 so enter the while loop and calculate rev. rev= 12*10 + 1%10 = 120+1 = 121 and n=1/10 = 0.
• Now the value of n is 0 so condition in the while loop becomes false hence the loop gets terminated.
• Now check if num is equal to rev or not. The value of num = 121 and the value of rev=121.
• Since both are equal print 121 is a palindrome number.

Time Complexity: O(log n)
The above program for checking palindrome number has a time complexity of O(n) as inside the while loop the value of n reduces to n/10 in each iteration.

Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

Runtime Test Cases

Testcase 1: In this case, we enter the number “121” as input to check whether it is a palindrome number or not.

```Enter the number: 121
121 is a palindrome number.```

Testcase 2: In this case, we enter the number “342” as input to check whether the entered number is palindrome number or not.

```Enter the number: 342
342 is not a palindrome number.```

Method 2: Palindrome Program in C using Iterative Function

In this approach, we check for Palindrome number using while loop.

Program/Source Code

Here is the source code of the C program to check whether a number is palindrome or not using iterative function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to Check Number is Palindrome or Not using Iterative.
*/

#include <stdio.h>

// function to reverse digits of num
int reverse(int num)
{
int rev = 0;
while (num > 0)
{
rev = rev * 10 + num % 10;
num = num / 10;
}
return rev;
}

//Check if the number is palindrome or not
int isPalindrome(int n)
{

//call the function reverse(n)
int rev = reverse(n);

// Check if rev and n are equal or not.
if (rev == n)
return 1;
else
return 0;
}

//main function
int main()
{
int n,res;
printf("Enter the number: ");
scanf("%d",&n);
res = isPalindrome(n);  //Call the function isPalindrome
if(res==1)
printf("%d is a palindrome number.",n);
else
printf("%d is not a palindrome number.",n);
return 0;
}```
Program Explanation

1. Take the number, n as input from the user.
2. Call the function isPalindrome() by passing the value of n and store the result obtained by the function, in the variable res.
3. Inside the isPalindrome() function call another function reverse() by passing the value n and store the result obtained by the function, in the variable rev.
4. Inside the reverse() function reverse the number using while loop by using the formula rev=rev*10 + num%10 and in each iteration recalculate the value of num as num/10 in each iteration.
5. After calculating rev return the value of rev. The returned value from the function reverse() gets stored in the variable rev in the function isPalindrome().
6. Now, in the function isPalindrome() check if rev is equal to n or not. If they are equal return the value as 1 else return the value as 0.
7. The value returned from isPalindrome() function gets stored in the variable res in the main function.
8. In the main function check if res is equal to 1 or not. If it is equal to 1 print the number as palindrome number.
9. Else, print the number as not a palindrome number.

Time Complexity: O(log n)
The above program for checking palindrome number has a time complexity of O(n) as inside the while loop the value of n reduces to n/10 in each iteration.

Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

Runtime Test Cases

Testcase 1: In this case, we enter the number “1001” as input to check whether it is a palindrome number or not.

```Enter the number: 1001
1001 is a palindrome number.```

Testcase 2: In this case, we enter the number “938” as input to check whether the entered number is palindrome number or not.

```Enter the number: 938
938 is not a palindrome number.```

Method 3: Palindrome Program in C using Recursion

In this approach, we check for Palindrome number using recursion.

Program/Source Code

Here is the source code of the C program to check whether a number is palindrome or not using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/* C Program to check number is Palindrome or Not using recursion*/

#include <stdio.h>

//Check if the number is palindrome or not
int isPalindrome(int n,int rev)
{
if (n==0)
return rev;

//calculate reverse of the number
rev = rev * 10 + n % 10;
isPalindrome(n/10,rev);
}

//main function
int main()
{
int n,res,rev=0;
printf("Enter the number: ");
scanf("%d",&n);
res = isPalindrome(n,rev);  //Call the function isPalindrome
if(res==n)
printf("%d is a palindrome number.",n);
else
printf("%d is not a palindrome number.",n);
return 0;
}```
Program Explanation

1. Take the number, n as input from the user.
2. Initialize variable res to store the result returned from isPalindrome() function and variable rev=0.
3. Call the function isPalindrome() by passing the value of n and rev and store the result obtained by the function, in the variable res.
4. Inside the isPalindrome() function check if n = 0 or not, if n=0 return the value of rev.
5. Else compute the value of rev by using the formula rev=rev*10 + n%10 and in each recursive call recompute the value of n as n=n/10.
6. Recursively call the function isPalindrome() by passing the value of rev and n/10 until n is not equal to 0.
7. When n=0 return the value of rev i.e., the reversed number and store it in the variable res in the main function.
8. Inside the main function then check if n is equal to res or not.
9. If n=res, print It is a palindrome number.
10. Else print, it is not a palindrome number.

Time Complexity: O(log n)
The above program for checking palindrome number has a time complexity of O(n) as the recursive function is called the number of times n is divided by 10.

Space Complexity: – O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

Runtime Test Cases

Testcase 1: In this case, we enter the number “121” as input to check whether it is a palindrome number or not.

```Enter the number: 121
121 is a palindrome number.```

Testcase 2: In this case, we enter the number “537” as input to check whether the entered number is palindrome number or not.

```Enter the number: 537
537 is not a palindrome number.```

Method 4: Palindrome Program in C using Bitwise Operator

In this approach, we checks whether the given number is palindrome or not using bitwise operator.

Program/Source Code

Here is source code of the C Program to Check whether the given Number is Palindrome or not using Bitwise Operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to Check whether the given Number is Palindrome
* or not using Bitwise Operator
*/

#include <stdio.h>
#include <string.h>
#define SIZE 8
/* Function Prototype */
int is_palindrome(unsigned char[]);

void main()
{
int num, num1 = 0, i = 0, j = SIZE - 1, res;
unsigned char c[SIZE];
printf("Enter a number(max 255)");
scanf("%d", &num);
num1 = num;
while (num != 0)
{
c[j] = num&1;
j--;
num = num>>1; /* Shifting right the given number by 1 bit */
}
printf("The number %d in binary is:", num1);
for (i = 0;i < SIZE;i++)
{
printf("%d", c[i]);
}
res = is_palindrome(c);    /* Calling Function */
if (res == 0)
{
printf("\nNUMBER IS PALINDROME\n");
}
else
{
printf("\nNUMBER IS NOT PALINDROME\n");
}
}

/* Code to check if the number is palindrome or not */
int is_palindrome(unsigned char c[])
{
char temp[SIZE];
int i, j, flag = 0;
for (i = 0, j = SIZE - 1;i < SIZE, j >= 0;i++, j--)
{
temp[j] = c[i];
}
for (i = 0;i < SIZE;i++)
{
if (temp[i] != c[i])
{
flag = 1;
}
}
return flag;
}```
Program Explanation

1. In this C program, we are reading the value of string using word character array variable. A palindrome is a word, phrase or sentence that reads the same backward or forward.
2. Check the values of a given string and reverse string are equal.
3. If else condition statement is used inside for loop if the condition is true then assign the value of ‘flag’ variable as 1.
4. Otherwise, if the condition is false then execute the else statement. Assign the value of ‘flag’ variable as 0 and exit the loop.
5. If else condition statement is used to check the value of ‘flag’ variable is equal to1.
6. If the condition is true then execute the statement and print the string is palindrome.
7. Otherwise, if the condition is false then execute the else statement and print as not a palindrome.

Runtime Test Cases

Testcase 1: In this case, we enter the number “153” as input to find binary number and check whether it is a palindrome number or not.

```Enter a number(max 255): 153
The number 153 in binary is:10011001
NUMBER IS PALINDROME```

Testcase 2: In this case, we enter the number “24” as input to find binary number and check whether the entered number is palindrome number or not.

```Enter a number(max 255): 24
The number 24 in binary is: 00011000
NUMBER IS PALINDROME```

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.