Posted by: Sourav | June 27, 2014

Find kaprekar numbers within a range in Java


import java.util.Scanner;
class kaprakar_number_range
{
public static void main(String args[])
{
    int p,q;
    int n;
    //int m;
    Scanner in = new Scanner(System.in);
        
        System.out.println(“input the higher range”);
        
        p = Integer.parseInt(in.nextLine());
        System.out.println(“input the lower range”);
        
        q = Integer.parseInt(in.nextLine());
        
    int temp,k,a=0,r,rem,quo;
     int temp2;
     

         
      for(int m=q;m<=p;m++)
      {
          n=m;
          temp2=n;
          temp=n;
          k=n*n;
          a=0;
            while(n>0)
            {
                r=n%10;
                a++;
                n=n/10;
            }
            
           // System.out.println(“a is “+a);
            rem=k%((int)Math.pow(10,a));
            quo=k/((int)Math.pow(10,a));
              
     if((rem+quo)==temp)
     {
            System.out.println( temp2+” is a kaprakar number”);
        }
            else
            {
              System.out.println(temp2+” is not kaprakar number”);
            }
        }
            }
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: