Fascinating Number ISC Computer Science 2021 Practical
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 zeroes and are to be ignored.
Example: 273
273 × 1 = 273
273 × 2 = 546
273 × 3 = 819
Concatenating the result we get, 273546819 which contains all digits from 1 to 9 exactly once.
Thus, 273 is a Fascinating number.
Accept two positive integers 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:
Example 1:
INPUT:
m = 100
n = 500
OUTPUT:
THE FASCINATING NUMBERS ARE:
192 219 273 327
FREQUENCY OF FASCINATING NUMBERS IS: 4
Example 2:
INPUT:
m = 900
n = 5000
OUTPUT:
THE FASCINATING NUMBERS ARE:
1902 1920 2019 2190 2703 2730 3027 3270
FREQUENCY OF FASCINATING NUMBERS IS: 8
Example 3:
INPUT:
m = 400
n = 900
OUTPUT:
THE FASCINATING NUMBERS ARE:
NIL
FREQUENCY OF FASCINATING NUMBERS IS: 0
Example 4:
INPUT:
m = 70
n = 450
OUTPUT:
INVALID INPUT
class Fascinating{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("m = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("n = ");
int n = Integer.parseInt(in.nextLine());
if(m < 100 || n < 100 || m > 9999 || n > 9990){
System.out.println("INVALID INPUT");
return;
}
int f = 0;
System.out.println("THE FASCINATING NUMBERS ARE:");
for(int i = m; i <= n; i++){
if(isFascinating(i)){
System.out.print(i + " ");
f++;
}
}
if(f == 0)
System.out.print("NIL\n");
else
System.out.println();
System.out.println("FREQUENCY OF FASCINATING NUMBERS IS: " + f);
}
public static boolean isFascinating(int num){
int x = num;
int y = num * 2;
int z = num * 3;
String c = x + "" + y + "" + z;
for(char i = '1'; i <= '9'; i++){
int count = 0;
for(int j = 0; j < c.length(); j++){
if(i == c.charAt(j))
count++;
}
if(count != 1)
return false;
}
return true;
}
}
Comments
Post a Comment