Array-promo

Question

Check whether a number is Kaprekar number without using string.
(A Kaprekar number is a number whose square when divided into two parts and such that sum of parts is equal to the original number and none of the parts has value 0.)

				
					ENTER A NUMBER: 45  
45 IS A KAPREKAR NUMBER
45*45 = 2025 and 20 + 25 is 45

ENTER A NUMBER:99
99 is a kaprekar number
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class kaprekarNumber
{
    public static void main()
    {
        int num=0;
        long count=0,sq=0,sum=0,firstPart=0,secondPart=0,temp=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("ENTER A NUMBER:");
        num=sc.nextInt();
        if(num>0)
        {
            count=0;
            sq=num*num;
            temp=sq;
            while(temp>0)
            {
                count++;
                temp=temp/10;
            }  
            temp=sq;
            firstPart=temp% (long)Math.pow(10,count-count/2);
            secondPart=temp/(long)Math.pow(10,count-count/2);
            sum=firstPart+secondPart;
            if((sum==num &&(firstPart!=0)&&(secondPart!=0))||num==1)
            {
                System.out.print(num+" is a kaprekar number");     
            }
            else
            {
                System.out.print(num+" is not a kaprekar number");    
            }
        }
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *