- Home
- Java Number Programs (ISC Classes 11 / 12)
Composite Magic Number Java Program
Java Number Programs (ISC Classes 11 / 12)
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 which has more than two factors.
For example:
Factors of 10 are: 1, 2, 5, 10
Magic number: A Magic number is a number in which the eventual sum of the digit 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. Display the number of composite magic integers that are in the range between m and n (both inclusive) and output them along with frequency, in the format specified below:
Sample Input:
m=10 n=100
Output: The composite magic numbers are 10,28,46,55,64,82,91,100
Frequency of composite magic numbers: 8
Sample Input:
m=120 n=90
Output: Invalid input
Answer
import java.util.Scanner;
public class KboatCompositeMagicNumber
{
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();
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);
}
}