Home / Programs / Command Line Program to Check if a Number is Palindrome or Not using C Programming language
Programming Example

Command Line Program to Check if a Number is Palindrome or Not using C Programming language

👁 1,001 Views
💻 Practical Program
📘 Step by Step Learning

Program to check whether the given number is a palindrome or not is discussed here. Any number is said to be a palindrome if the original number and the reverse of the original number are the same.

For example, 1234321 is a palindrome.

  • Original number = 1234321
  • The reverse of the number = 1234321

Algorithm to check whether a number is a palindrome or not

  • Input the number. 
  • Find the reverse of the number.
  • If the reverse of the number is equal to the number, then return true. Else, return false.

Program Code

#include <stdio.h>

int main(int argc, char *argv[])
{
            int num, reverse_num=0,remainder,temp;
            num = atol(argv[1]);
            temp=num;

            while(temp!=0)
           {
            remainder=temp%10;
            reverse_num=reverse_num*10+remainder;
            temp/=10;
           }
   
   if(reverse_num==num)
            printf("%d is a palindrome number",num);
   else
            printf("%d is not a palindrome number",num);
   return 0;
}

Output

11

11 is a palindrome number
<hr>

121

121 is a palindrome number

Explanation

Program without command line arguments


// program by atnyla coder

#include<stdio.h>
void main()
{

int number, temp, remainder;
int sum = 0;
printf("Enter a number to check for palindrome. ");
scanf("%d", &number);

temp = number;

while(temp != 0)
{
	remainder = temp % 10;
	sum = sum*10 + remainder;
	temp = temp / 10;
}

if(sum == number)
{
	printf("Number is palindrome");
}
else{
	printf("Number is not palindrome");
}

}

Palindrome or not using a recursive approach


#include<stdio.h>

int is_Palindrome(int );

int n;

int main()
{
	int palindrome;
	printf("\n\nEnter a number : ");
	scanf("%d", &n);
	palindrome = is_Palindrome(n);
	
	if(palindrome == 1)
		printf("\n%d is palindrome\n", n);
	else
		printf("\n%d is not palindrome\n", n);

return 0;
}

int is_Palindrome(int aj)
{
	static int sum = 0;
	if(aj != 0)
	{
		sum = sum *10 + aj%10;
		is_Palindrome(aj/10); // recursive call
	}
	else if(sum == n)
		return 1;
	else
		return 0;
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.