algorithm for composite magic no. please in Java
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
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
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);
Answer:
Sure, a composite magic number is a number that is both composite (not a prime number) and a magic number (a number whose sum of digits eventually equals 1 by recursively adding the digits until a single digit is obtained).
Here's an example of how you might implement this in Java:
```java
public class CompositeMagicNumber {
// Function to check if a number is composite
static boolean isComposite(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return true;
}
}
return false;
}
// Function to find the sum of digits until a single digit is obtained
static int getSum(int n) {
int sum = 0;
while (n > 0 || sum > 9) {
if (n == 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
// Function to check if a number is a composite magic number
static boolean isCompositeMagicNumber(int n) {
if (isComposite(n)) {
int sum = getSum(n);
return sum == 1;
}
return false;
}
public static void main(String[] args) {
int num = 10; // You can change this number to test
if (isCompositeMagicNumber(num)) {
System.out.println(num + " is a composite magic number.");
} else {
System.out.println(num + " is not a composite magic number.");
}
}
}
```
This code defines methods to check if a number is composite, find the sum of its digits, and determine if it's a composite magic number. You can change the `num` variable in the `main` method to test different numbers.