-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCompositeMagicNumber.java
52 lines (45 loc) · 1.37 KB
/
CompositeMagicNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
public class CompositeMagicNumber
{
public static void main(String []args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter m: ");
int m = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
in.close();
if (m < 1 || n < 1 || m > n) {
System.out.println("Invalid input");
return;
}
System.out.println("The composite magic numbers are:");
int count = 0;
for (int i = m; i <= n; i++) {
boolean isComposite = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isComposite = true;
break;
}
}
if (isComposite && i != 1) {
int num = i;
while (num > 9) {
int sum = 0;
while (num != 0) {
int d = num % 10;
num /= 10;
sum += d;
}
num = sum;
}
if (num == 1) {
count++;
System.out.print(i + " ");
}
}
}
System.out.println();
System.out.println("Frequency of composite magic numbers: " + count);
}
}