Magic Composite
Magic Composite
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 that has more than two factors.
Magic number:
For example: IO
Factors are: l, 2, 5, IO
A magic number is a number in which the eventual sum of the digits is equalto I
For example: +0=1
Accept two positive integers m and n, where m is less than n as user input. Display the number
of
Composite magic integers that are in the range between m and n (both inclusive) and output
them along with the frequency, in the format specified below.
Test your program with the sample data and some random data:
Example I:
INPUT: m=10
n=lOO
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE:
10, 28, 46, 55, 64, 82, 91, 100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8
n = 99
Example 2:
INPUT: m = 1200
n = 1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE:
1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9
ALGORITHM
1. START
2. Initialize a Scanner object to read user input.
3. Prompt the user to input the range n and m.
4. Read the values n and m.
5. Initialize a counter c to 0.
6. Loop from i = n to i <= m:
7. Check if i is a magic number using the isMagic function.
8. Check if i is a composite number using the isComposite function.
9. If both conditions are true, print i and increment the counter c.
10.Print the total number of magic composite numbers found.
11.Close the Scanner object.
12.End
Function isMagic(a):
13.Initialize number to a and sum to 0.
14.While number is greater than or equal to 10:
15.Reset sum to 0.
16.While number is greater than 0:
17.Extract the last digit of number.
18.Add the digit to sum.
19.Remove the last digit from number.
20.Set number to sum.
21.Return true if number equals 1, else return false.
Function isComposite(a):
22.If a is less than or equal to 1, return false.
23.Initialize a counter c to 0.
24.Loop from i = 1 to i <= a:
25.If a is divisible by i, increment c.
26.Return true if c is greater than 2, else return false.
27.STOP.
CODE
import java.util.*;
class magic_composite
{
public static void main()
{
Scanner obj = new Scanner(System.in);
System.out.println("Input the range");
int n = obj.nextInt();
int m=obj.nextInt();
int c=0;
for(int i =n;i<=m;i++)
{
if(ismagic(i) && iscomposite(i))
{
System.out.println(i);
c++;
}
}
System.out.println("No of magic composite no. "+c);
}