Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
754 views
asked by Advisor (7.1k points) 11 23 43

2 Answers

0 votes
If a number, which when read in both forward and backward way is same, then such a number is called a palindrome number.

Program:

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

 /* logic */
 while (n > 0)
{
  rem = n % 10;
  rev = rev * 10 + rem;
  n = n / 10;
 }

 if (n1 == rev)
{
  printf("Given number is a palindromic number");
 }
 else
{
  printf("Given number is not a palindromic number");
 }
 return 0;
}


Output:
Enter any number: 121
Given number is a palindrome
answered by Expert (12.3k points) 7 13 26
edited by
0 votes
If a number, which when read in both forward and backward way is same, then such a number is called a palindrome number.

Program:

#include <stdio.h>

int main() {

int n, n1, rev = 0, rem;

printf("Enter any number: \n");

scanf("%d", &n);

n1 = n;

/* logic */

while (n > 0){

rem = n % 10;

rev = rev * 10 + rem;

n = n / 10;

}

if (n1 == rev){

printf("Given number is a palindromic number");

}

else{

printf("Given number is not a palindromic number");

}

return 0;

}

Output:

Enter any number: 121

Given number is a palindrome

Explanation with an example:

Consider a number n=121, reverse=0, remainder;

number=121

now the while loop is executed /* the condition (n>0) is satisfied */

/* calculate remainder */

remainder of 121 divided by 10=(121%10)=1;

now reverse=(reverse*10)+remainder

=(0*10)+1 /* we have initialized reverse=0 */

=1

number=number/10

=121/10

=12

now the number is 12, greater than 0. The above process is repeated for number=12.

remainder=12%10=2;

reverse=(1*10)+2=12;

number=12/10=1;

now the number is 1, greater than 0. The above process is repeated for number=1.

remainder=1%10=1;

reverse=(12*10)+1=121;

number=1/10 /* the condition n>0 is not satisfied,control leaves the while loop */

Program stops here. The given number=121 equals the reverse of the number. Thus the given number is a palindrome number.
answered by Expert (13.2k points) 29 44 63

Related questions

+2 votes
4 answers 4k views
4k views asked by Smita Advisor (7.1k points) 11 23 43
0 votes
2 answers 5.7k views
5.7k views asked by sagar Expert (12.3k points) 7 13 26
0 votes
1 answer 448 views
+1 vote
3 answers 3.2k views
3.2k views asked by Smita Advisor (7.1k points) 11 23 43
...