We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Modified Kaprekar Numbers
  5. Discussions

Modified Kaprekar Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

    You are viewing a single comment's thread. Return to all comments →

  • iamsaurabhverma
    3 months ago+ 0 comments

    i dont know whats wrong with my code?

    public static void kaprekarNumbers(int p, int q) {
        // Write your code here
            for (int i = p; i < q; i++) {
                if (checkResult(i))
                    System.out.print(i + " ");
            }
        }
            private static boolean checkResult(int n) {
            int temp = n;
            n = n * n;
            int c = count(n);
            int length = c / 2;
    //        System.out.println(length);
            String r = "";
            while (length > 0) {
                int d = n % 10;
                r = d + r;
                n = n / 10;
                length--;
            }
            int res = Integer.parseInt(r) + n;
            if (res == temp)
                return true;
            else
                return false;
        }
            private static int count(int n) {
            int c = 0;
            do {
                c++;
                n = n / 10;
            } while (n != 0);
            return c;
        }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy