Tuesday, March 23, 2010

Check For Palindrome & Perfect Number

import java.io.*;

public class palindrome
{
  public static void main(String args[]) throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
  palindrome ab = new palindrome();
  ab.show();
  }
   
  void show() throws IOException
  { InputStreamReader read = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(read);
  System.out.println("Enter 1 for checking for a palindrome, 2 to check for perfect number");
  int a = Integer.parseInt(br.readLine());
  System.out.println("Enter number to check");
  int aa = Integer.parseInt(br.readLine());
  switch(a)
  { case 1: palin(aa); break;
  case 2: pno(aa); break;
  default: System.out.println("Wrong numeber");
  }
  }
  void palin(int x)
  { int remain, reverse = 0; int original=x;
  while(x>0)
  { remain = x%10;
  reverse = reverse*10 + remain;
  x=x/10;
  }
  if(reverse==original)
  System.out.println("It is palindrome");
  else System.out.println("Sorry");
  }
  void pno(int x)
  { int sum=0; 
  for(int i=1;i  { if(x%i==0)
  sum=sum+i;
  }
  if(sum==x) 
  System.out.println("It is perfect number");
  else 
  System.out.println("It is not a perfect number");
  }
}


   

No comments:

Post a Comment