Tuesday, April 30, 2013

Program to check Kaprekar Number


/**
 *This program checks that whether a number is Kaprekar Number or not.
 * @author : Nitendra Kumar
 */

import java.util.Scanner;
public class Kaprekar
{
   public static int num;
   Scanner in=new Scanner(System.in);
       
    public void fngetnum()
    {
        System.out.println("Enter a number");
        num=in.nextInt();
    }

    public int fnsumdigits(int num)
    {
     int snum=num*num;
     int len=String.valueOf(snum).length();
     int mid=len/2;
     int digleft=mid;
     int digright=len-mid;
     
     int lpart=snum/(int)Math.pow(10,digright);
     int rpart=snum%(int)Math.pow(10,digright);
     int sum=lpart+rpart;
     if(sum==num)
      return 1;
     else
      return 0;     
    }
    
    void fncheckkarpekar()
    {
        int a=fnsumdigits(num);
        if(a==1)
          System.out.println("Number is Kaprekar Number");
        else
          System.out.println("Number is not Kaprekar Number");
        }
        
    public static void main()
    {
        Kaprekar ob=new Kaprekar();
        ob.fngetnum();
        ob.fncheckkarpekar();
    }
}

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

No comments:

Post a Comment

Ur comments r most welcome...