0

# How do you write a program in C to check whether a number is a palindrome or not?

Updated: 12/21/2022

Wiki User

13y ago

The simplest method is to reverse the digits of the original number and check to see if the reversed number is equal to the original. This is not an efficient way of doing it but, for ints or long ints, it works well enough.

void main()

{

// get the number to check

long int n;

printf("ENTER A NUMBER: ");

scanf("%ld",&n);

long int n1,mod;

// make a copy of the original number in n1

n1=n;

// store the reversal in rev

long int rev=0;

while(n>0)

{

// grab the rightmost digit of n

mod = n%10;

// shift all digits of rev left, then add in the new digit

rev = rev * 10 + mod;

// shift all digits of n to the right (removing the rightmost one)

n = n / 10;

}

// after the loop, rev will have the opposite digit ordering of n1;

// if these two values are equal, then we have a palindrome

if (n1 == rev)

printf("%ld is a palindrome\n",n1);

else

printf("%ld is not a palindrome\n",n1);

}

Alternate method:

Split the number into its digits, as on the example below: Number: 3592 1st digit: 3592%10 = 2 3592/10=359. ... 2nd digit: 359%10 = 9 etc. Store these into an integer array, then check if all digits correspond to digits at the reflected position.

Wiki User

13y ago

Earn +20 pts
Q: How do you write a program in C to check whether a number is a palindrome or not?
Submit
Still have questions?
Related questions

### Write a PHP program to check whether the string is palindrome or not?

You can do this: &lt;?php if ( \$word === strrev( \$word ) ) { echo "The word is a palindrome"; } else { echo "The word is not a palindrome"; }

### Write a program using method plainto check whether a string is a palindrome or not A palindrome is a string that reads the same from left to right and vice-versa?

Palindrome number is a number like 121 which remains the same when its digits are reversed. To find this number in a simple java program, just follow the below way. sum = 0; while(n&gt;0) { r=n % 10; sum=concat(r); n=n / 10; } print r;

### Program to check that given string is palindrome or not in C?

/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }

### Write a program By using if else statement to read a number and check whether it is positive or negative?

write a c++program by using if statement to read a number and check whether it is positive or negative

### How do you write a program in C to check whether a word is a palindrome or not?

It is a simple program. i think u may understand it :#include#include#includevoid main(){char s[10]=answers.com;char x[10];int a;clrscr();strcpy(x,s);strrev(s);a=strcmp(s,x);if(a==0){printf("the entered string is palindrome");}else{printf("the entered string is not palindrome");}output:given string is not palindrome

### Write a PHP program to check whether the number is palindrome or not?

This program only suits PHP. If you want a proper one try C program for it available on web &lt;body&gt; &lt;?php if(isset(\$_POST['submit'])) { \$text = \$_POST['text']; \$string = mysql_real_escape_string(\$text); \$invert = strrev(\$string); if(\$string == \$invert) { echo "&lt;br&gt;Given number is a palindrome!!"; } else { echo "&lt;br&gt;Given number is not a palindrome!!"; } } ?&gt; &lt;form method="post"&gt; &lt;input type="text" name="text" id="text" /&gt; &lt;input type="submit" name="submit" value="Submit" id="submit" onclick="&lt;?php \$_SERVER['PHP_SELF']; ?&gt;" /&gt; &lt;/form&gt; &lt;/body&gt;

### How do you write a simple program in c to check whether a number is a palindrome or not?

unsigned reverse (unsigned n) { unsigned r = 0; while (n) { r *= 10; r += (n%10); n /= 10; } return r; } bool is_palindrome (unsigned n) { return n == reverse (n); } int main () { unsigned num; printf ("Enter a number: "); scanf ("%d", num); if (is_palindrome (num)) printf ("%d is a palindrome\n"); else printf ("%d is not a palindrome\n"); return 0; }

### C program to check whether the number and its reverse are same or not?

#include &lt;stdio.h&gt; #include&lt;conio.h&gt; void main () { int a,r,n,sum=0; clrscr(); printf("enter the number"); scanf("%d",&amp; n); c=n while (n!=0) { a=n%10; n=n/10; sum=sum*a+a } printf("the reverse is %d \n",sum); if(c==sum) printf("\n the given number is palindrome"); else printf("\n the given number is not a palindrome"); getch(); }

### Lab manual in mca 1st sem from ignou?

write a program in C to check whether a given number is apalindrome or not

### Could you write a assembly language program in tasm To check whether a given number present in a sequence of given memory location containing the string to be checked in 8086?

8086 assembly language program to check wether given number is perfect or not

### Write a c program that reverse a given integer number and check whether the number is palindrome or not?

#include &lt;stdio.h&gt; #include &lt;string.h&gt; #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 -&gt;palindrome, flag =1 -&gt;nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; } #include &lt;stdio.h&gt; #include &lt;string.h&gt; #define N 100 #define PALINDROME 0 #define NONPALINDROME 1 /*Program that tells you whether what you enter is a palindrome or not*/ char pal[N]; //input line int i; //counter int k; //counter int tag; int flag = PALINDROME; /*flag=0 -&gt;palindrome, flag =1 -&gt;nonpalindrome*/ int main() { printf("Enter something: \n"); scanf("%s", pal); tag = strlen(pal); /*determine length of string*/ /* pointer running from the beginning and the end simultaneously looking for two characters that don't match */ /* initially assumed that string IS a palindrome */ /* the following for loop looks for inequality and flags the string as a non palindrome the moment two characters don't match */ for (i=0,k=tag-1; i=0; i++,k--) { if(pal[i] != pal[k]) { flag=NONPALINDROME; break; } } if(flag == PALINDROME) { printf("This is a palindrome\n"); } else { printf("This is NOT a palindrome\n"); } return 0; }

### How you write a program in c plus plus to print plaindromic numbers from 1 to n?

To check if a number is a palindrome, reverse the number and see if it is equal to the original number. If so, the number is a palindrome, otherwise it is not. To reverse a number, use the following function: int reverse(int num, int base=10) { int reverse=0; while( num ) { reverse*=base; reverse+=num%base; num/=base; } return(reverse); }