Home  >  Article  >  Backend Development  >  Use C language to check whether the input value is a palindrome

Use C language to check whether the input value is a palindrome

PHPz
PHPzforward
2023-09-03 12:37:071248browse

Use C language to check whether the input value is a palindrome

A palindrome is any word, number, sentence or other sequence of characters that is the same whether read front to back or back to front.

In this programming, we try to enter a number from the console and assign the number to a temporary variable.

If the number is greater than zero, apply the logic given below:

while(n>0){
   r=n%10;
   sum=(sum*10)+r;
   n=n/10;
}

If temp=sum, then the given number is a palindrome. Otherwise, it is not a palindrome.

Example

The following is a C program for verifying whether a value is a palindrome:

#include<stdio.h>
#include<conio.h>
void main(){
   int n, r, sum=0, temp;
   printf("Enter a number: ");
   scanf("%d",&n);
   temp=n;
   while(n>0){
      r=n%10;
      sum=(sum*10)+r;
      n=n/10;
   }
   if(temp==sum)
      printf("It is a palindrome number!");
   else
      printf("It is not a palindrome number!");
   getch();
}

Output

When executing the above program , it produces the following result −

12345
It is not a palindrome number

The above is the detailed content of Use C language to check whether the input value is a palindrome. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete