Check for Fascinating Number. 2nd
import java.util.*;
class Fasc {
long num;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextLong();
sc.close();
}
boolean is_fasc() {
if (num < 100)
return false;
long n2 = num * 2, n3 = num * 3;
int[] cnt = new int[10];
String join = "" + num + n2 + n3;
num = Long.parseLong(join);
while (num != 0) {
int dig = (int) (num % 10);
cnt[dig]++;
num /= 10;
}
for (int x = 1; x < 10; x++) // we have to ignore 0s
if (cnt[x] != 1)
return false;
return true;
}
public static void main(String[] args) {
Fasc a = new Fasc();
a.input();
System.out.println("Is " + (a.is_fasc() ? "" : "not ") + "a Fascinating number");
}
}
| Name | Type | Uses |
|---|---|---|
| global | ||
| num | long | to store entered number |
| void main() | ||
| a | Fasc | Object to call the function |
| boolean isfasc() | ||
| n2 | long | To store double of num |
| n3 | long | To store triple of num |
| cnt | int[] | Integer array of size 10 to store digit freequency |
| join | String | To store concated num, n2 and n3 |
| dig | int | To store extracted digit |
| void input() | ||
| sc | Scanner | Object to take User Input |