Tuesday, April 30, 2013

Program to check kaprekar Number(By using string)


/**
 * This program checks Kaprekar Number with the help of string
 * @author: Nitendra Kumar
 **/

import java.io.*;
public class Kaprekar1
{
   public static void main() throws IOException
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter a Number : ");
 int num=Integer.parseInt(br.readLine()); //Inputting the number
 int sq=num*num; //finding the square of the number
 String s=Integer.toString(sq); //converting the square into a String

 if(sq<=9)
 s="0"+s;

 int l=s.length(); //finding the length (i.e. no. of digits in the square).
 int mid=l/2; //finding the middle point
 String left=s.substring(0,mid); //extracting the left digits from the square
 String right=s.substring(mid); //extracting the right digits from the square
 int x=Integer.parseInt(left); //converting the left String into Integer
 int y=Integer.parseInt(right); //converting the right String into Integer

 if(x+y == num) //comparing sum with number
 System.out.println(num+" is a Kaprekar Number");
 else 
 System.out.println(num+" is Not a Kaprekar Number");
}
}

/*------------------------Program Developed by: Nitendra Kumar------------------------*/
//For more details visit http://javawithnitendra.blogspot.in

No comments:

Post a Comment

Ur comments r most welcome...