Code with Vinay

ISC practical papers › ISC 2010 › Question 2

Kaprekar Number

ISC 2010 Computer Science Practical, Question 2 · Number programs

The question

A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits. If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297, ...

Example 1: 9 9*9 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8 Sum = 1 + 8 = 9, i.e. equal to the number.

Example 2: 45 45*45 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20 Sum = 25 + 20 = 45, i.e. equal to the number.

Example 3: 297 297*297 = 88209, right-hand piece of 88209 = 209 and left hand piece of 88209 = 88 Sum = 209 + 88 = 297, i.e. equal to the number.

Given the two positive integers p and q, where p < q, write a program to determine how many Kaprekar numbers are there in the range between p and q (both inclusive) and output them.

The input contains two positive integers p and q. Assume p < 5000 and q < 5000. You are to output the number of Kaprekar numbers in the specified range along with their values in the format specified below:

SAMPLE DATA:

INPUT:
p = 1 q = 1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:-
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8

Question: ISC 2010 Computer Science Paper 2 (Practical), Question 2, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2010Q2.java Download GitHub
import java.util.*;

class Kaprekar {
    private int p, q, frequency;
    String output;

    public Kaprekar(int p, int q) {
        this.p = p;
        this.q = q;
        output = "";
        processRange();
    }

    private void processRange() {
        for (int i = p; i <= q; i++) {
            if (isKaprekarNumber(i)) {
                output += i + ", ";
                frequency++;
            }
        }
        output = output.substring(0, output.length() - 2);
    }

    private static boolean isKaprekarNumber(int n) {
        int square = n * n, divisor = 1;
        for (int temp = n; temp > 0; temp /= 10) {
            divisor *= 10;
        }
        return n == (square / divisor) + (square % divisor);
    }

    public String toString() {
        return output;
    }

    public int getFrequency(){
        return frequency;
    }
}

class ISC2010Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT:");
        int p,q;
        System.out.print("p = ");
        p=in.nextInt();
        System.out.print("q = ");
        q=in.nextInt();
        Kaprekar obj = new Kaprekar(p,q);
        System.out.println("OUTPUT:\nTHE KAPREKAR NUMBERS ARE:-");
        System.out.println(obj);
        System.out.println("FREQUENCY OF KAPREKAR NUMBERS IS: "+obj.getFrequency());
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers