Code with Vinay

ISC practical papers › ISC 2021 › Question 1

Fascinating Number In A Range

ISC 2021 Computer Science Practical, Question 1 · Number programs

The question

A fascinating number is one which when multiplied by 2 and 3 and then, after the results are concatenated with the original number, the new number contains all the digits from 1 to 9 exactly once. There can be any number of zeros and are to be ignored.

Example: 273 273 * 1 = 273 273 * 2 = 546 273 * 3 = 819 Concatenating the results, we get 273546819 which contains all digits from 1 to 9 exactly once. Thus 273 is a fascinating number.

Accept two positive intgers m and n, where m must be less than n and the values of both 'm' and 'n' must be greater than 99 and less than 10000 as user input. Display all fascinating numbers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:

Test your program for the following data and some random data.

Example 1:
INPUT:  m = 100
        n = 500
OUTPUT: THE FASCINATING NUMBERS ARE:
192 219 273 327
THE FREQUENCY OF FASCINATING NUMBER IS: 4
Example 2:
INPUT:  m = 900
        n = 5000
OUTPUT: THE FASCINATING NUMBERS ARE:
1902 1920 2019 2190 2703 2730 3027 3270
THE FREQUENCY OF FASCINATING NUMBER IS: 8
Example 3:
INPUT:  m = 400
        n = 900
OUTPUT: THE FASCINATING NUMBERS ARE:
NIL
THE FREQUENCY OF FASCINATING NUMBER IS: 0
Example 4:
INPUT:  m = 70
        n = 450
OUTPUT: Invalid range

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

Java solution

ISC2021Q1.java Download GitHub
import java.util.*;
class Fascinating{
    private int m, n, frequency;
    private static int digits[];
    private String output;
    public Fascinating(int m, int n){
        this.m=m;
        this.n=n;
        frequency=0;
        process();
    }
    private void process(){
        output="";
        for( int i=m; i<=n; i++){
            if(isFascinating(i)){
                frequency++;
                output += i+ " ";
            }
        }
        output=output.trim();
    }
    private static boolean isFascinating(int num){
        digits = new int[9];
        boolean status = inspectDigits(num) && inspectDigits(num*2) && inspectDigits(num*3);
        if(status==false) return false;
        for( int count: digits){
            if(count!=1) return false;
        }
        return true;
    }
    private static boolean inspectDigits(int product){
        int digit;
        for( int temp=product;temp>0; temp/=10){
            digit=temp%10;
            if(digit>0){
                if(digits[digit-1]==1) return false;
                digits[digit-1]=1;
            }
        }
        return true;
    }
    public int getFrequency(){
        return frequency;
    }
    public String toString(){
        String header = "THE FASCINATING NUMBERS ARE:\n";
        if(output=="") return header+"NIL";
        else return header+output;
    }
}
class ISC2021Q1{
    public static void main(String[] args) {
        Scanner in = new Scanner( System.in );
        System.out.print("INPUT:\tm = ");
        int m = in.nextInt();
        System.out.print("\tn = ");
        int n = in.nextInt();
        if(m>n || m<=99 || n<=99 || m>=10000 || n>=10000){
            System.out.println("OUTPUT:\tInvalid Range");
        }else{
            Fascinating obj = new Fascinating(m,n);
            System.out.println("OUPUT:\t"+obj);
            System.out.println("THE FREQUENCY OF FASCINATING NUMBER 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