Adobe Interview Question
Software Engineer in TestsCountry: India
I guess last comparison would be
return((m==a)?true:false)
instead of
return((m==n)?true:false)
few more mistakes are there...
in while loop n never became < or = 0 (smallest value of n can be 1)
value of n changed in the while loop so you can not compare with m in return statement ...
etc.. those are the small changes...
so i re write the code
bool IsPalindrom(int a)
{
int m = 0;
if(a<0) a = -a;
int n = a;
while(n >9)
{
m += n%10;
m *= 10;
n /= 10;
}
m += n;
return(m==a?true:false);
}
Why do you write this return((m==n)?true:false); ?
Why don't you write return m == n; ?
Here's another error. The evaluation of -x results in signed overflow when x is INT_MIN. In theory, this is undefined behavior, and the compiler can do anything it likes under those circumstances. In practice, it results in -x == x, which breaks invariants in the rest of your code.
This is a common mistake in routines which (like this one) take absolute values to normalize the input. The easy fix: unsigned int n = (unsigned int) a, m = 0; and remove 'if (a < 0) n = -a;' entirely.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[80];
memset(a, '\0', 80);
printf("Enter a integer :");
scanf("%s", a);
int len = strlen(a);
int i = 0;
bool flag = true;
if(a[0] == '-')
i++;
for(int j = len-1;i <= len/2; i++, j--)
{
if(a[i] != a[j])
{
flag = false;
break;
}
}
printf("\n%d", flag);
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PalinTest {
public static void main(String[] args) {
System.out.println("Please Enter the number");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
try {
String integer = bufferedReader.readLine();
int j = integer.length();
int count=1;
for (int i = 0, l = j - 1; i < j - 1; i++, l--,count++) {
if (integer.charAt(i) != integer.charAt(l)) {
System.out.println("not a plaindrome");
break;
}
continue;
}
if(count==j)
{
System.out.println("Entered number is a palindrome");
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sorry forgot to handle negative integers..here is modified one:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PalinTest {
public static void main(String[] args) {
System.out.println("Please Enter the number");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
try {
String integer = bufferedReader.readLine();
int j = integer.length();
int count = 1;
for (int i = 0, l = j - 1; i < j - 1; i++, l--, count++) {
if (integer.charAt(0) == '-') {
continue;
}
if (integer.charAt(i) != integer.charAt(l)) {
System.out.println("not a plaindrome");
break;
}
continue;
}
if (count == j) {
System.out.println("Entered number is a palindrome");
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sorry forgot to handle negative integers..here is modified one:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PalinTest {
public static void main(String[] args) {
System.out.println("Please Enter the number");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(System.in));
try {
String integer = bufferedReader.readLine();
int j = integer.length();
int count = 1;
for (int i = 0, l = j - 1; i < j - 1; i++, l--, count++) {
if (integer.charAt(0) == '-') {
continue;
}
if (integer.charAt(i) != integer.charAt(l)) {
System.out.println("not a plaindrome");
break;
}
continue;
}
if (count == j) {
System.out.println("Entered number is a palindrome");
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,k,r,rev=0;
printf("Enter the number\n");
scanf("%d",&n);
if(abs(n)<10)
printf("%d is a palindrome\n");
else
{
k=n;
while(k)
{
r=k%10;
rev=rev*10+r;
k=k/10;
}
if(rev==n)
printf("%d is a palindrome\n",n);
else
printf("%d is not a palindrome\n",n);
}
return 0;
}
bool palindrome(int a)
- Ali_BABA February 15, 2012{
int n=a,m=0;
if(a<0)n=-a
while(n>0)
{
m *= 10;
m += n%10;
n /= 10;
}
return((m==n)?true:false)
}