Karprekar Number

Take a Positive whole number n that has d number of digits. Take the square n and separate the result into two pieces. a right-hand piece that has d digits and a left-hand piece that has either d or d-1 digits. Add these two pieces together. If the result is n, then n is a Karprekar Number.







import java.util.*;
class Kaprekar_Number
{
    public static void main(String args[])
    {
        int n, m, sq,d=0,a,b;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Number  = ");
        n=sc.nextInt();
        sq=n*n;
        m=n;
        while(m>0)
        {
            d++;
            m=m/10;
        }
        a=sq%(int)Math.pow(10,d);   
        b=sq/(int)Math.pow(10,d);   
        if((a+b)==n)
             System.out.println("The Number is Kaprekar Number");
        else
             System.out.println("The Number is Not a Kaprekar Number");
    }
}

No comments

Powered by Blogger.