Home > Backend Development > C++ > 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
Release: 2023-09-03 12:37:07
forward
1287 people have browsed it

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;
}
Copy after login

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();
}
Copy after login

Output

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

12345
It is not a palindrome number
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template