Array-promo

Question

Print  Kaprekar number in given range without using string.
(A Kaprekar number is a number whose square when divided into two parts and such that the 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 LOWER RANGE:1
ENTER A UPPER RANGE:1000
KAPREKAR NUMBERS:
1 9 45 55 99 297 703 999
				
			

Share code with your friends

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

Code

				
					import java.util.Scanner;
public class kaprekarNumberInGivenRange
{
    public static void main()
    {

        int i=0,lowerRange=0,upperRange=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 LOWER RANGE:");
        lowerRange=sc.nextInt();
        System.out.print("ENTER A UPPER RANGE:");
        upperRange=sc.nextInt();

        System.out.println("KAPREKAR NUMBERS:");
        for(i=lowerRange;i<=upperRange;i++)
        {   
            if(i>0)
            {

                count=0;

                sq=i*i;
                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==i &&(firstPart!=0)&&(secondPart!=0))||i==1)
                {
                    System.out.print(i+" ");
                    
                }  

            }
        }
        
    }
}


				
			

Coding Store

Leave a Reply

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