ISC practical papers › ISC 2014 › Question 1
Composite Magic Number
ISC 2014 Computer Science Practical, Question 1 · Number programs
The question
A Composite Magic number is a positive integer which is composite as well as a magic number.
Composite number: A composite number is a number that has more than two factors. For example: 10, factors are: 1, 2, 5, 10
Magic number: A magic number is a number in which the eventual sum of the digits is equal to 1. For example: 28=2+8=10=1+0=1
Accept two positive integers m and n, where m is less than n as user input. Display the number of Composite Magic integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format specified below. Example 1:
Input: m=10
n=100
Output: THE COMPOSITE INTEGERS ARE:
10, 28, 46, 55, 64, 82, 91, 100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8
Example 2:
Input: m=1200
n=1300
Output: THE COMPOSITE INTEGERS ARE:
1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9
Example 3:
Input: m = 120
n=99
Output: INVALID INPUT
Question: ISC 2014 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
import java.util.*;
class CompositeMagicNumber {
private int m, n, frequency;
String output;
public CompositeMagicNumber(int m, int n) {
this.m = m;
this.n = n;
}
public void process() {
frequency = 0;
output = "THE COMPOSITE INTEGERS ARE:\n\t";
for (int i = m; i <= n; i++) {
if (isMagicNumber(i) && isCompositeNumber(i)) {
frequency++;
output+=i+", ";
}
}
output=output.substring(0,output.length()-2);
}
public String toString() {
return output;
}
public int getFrequency() {
return frequency;
}
private static boolean isPrimeNumber(int n) {
if (n < 2)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
else {
// number is odd
int limit = (int) Math.sqrt(n);
for (int divisor = 3; divisor <= limit; divisor += 2) {
if (n % divisor == 0)
return false;
}
}
return true;
}
private static boolean isCompositeNumber(int n) {
return !isPrimeNumber(n);
}
private static boolean isMagicNumber(int n) {
return n % 9 == 1;
}
}
class ISC2014Q1 {
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();
System.out.print("OUTPUT:\t");
if (m < n) {
CompositeMagicNumber obj = new CompositeMagicNumber(m, n);
obj.process();
System.out.println(obj);
System.out.println("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: " + obj.getFrequency());
} else {
System.out.println("INVALID INPUT");
}
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.