write a program to check the given number is palintron or not
Answers
------------------------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}
Correct Question:
Write a program to check whether the given number is a palindrome or not.
Answer:
import java.util.Scanner ;
public class Palindrome
{
public statis void main (String args[ ] )
{
Scanner sc = new Scanner (System.in) ;
System.out.print("Enter a number to be checked : ") ;
int number = sc.nextInt( ) ;
int reversedNumber = 0, temp = 0, savedNumber ;
savedNumber = number;
do {
temp = number % 10 ;
number = number / 10 ;
reversedNumber = reversedNumber * 10 + temp ;
} while (number! = 0) ;
if ( reversedNumber = = savedNumber)
System.out.println(savedNumber + "is a palindrome") ;
else
System.out.println(savedNumber + "not a palindrome") ;
}
}
Ouput:
Enter a number to be checked : 3003
3003 is a palindrome
Palindrome:
A palindrome reads the same from both sides,
Eg: 1331 is a palindrome number