SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Composite Magic Number Program in Java and Python

20 February 2014

Composite magic number program with algorithm, explanation, Java solution and simple Python solution for ISC students.

Question:

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. For example, 10 has factors 1, 2, 5 and 10.

Magic number: A magic number is a number in which the eventual sum of digits 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 all Composite Magic integers between m and n, both inclusive, along with their frequency.

Example 1 INPUT: m = 10 n = 100 OUTPUT: THE COMPOSITE MAGIC INTEGERS ARE: 10, 28, 46, 55, 64, 82, 91, 100 FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8 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 Example 3 INPUT: m = 120 n = 99 OUTPUT: INVALID INPUT

Algorithm:

Step 1: Start.

Step 2: Accept lower limit m and upper limit n.

Step 3: If m is not less than n, display INVALID INPUT and stop.

Step 4: Initialize frequency counter c to 0.

Step 5: Repeat for every number i from m to n.

Step 6: Check whether i is composite by counting its factors.

Step 7: Find repeated digit sum of i until the result becomes a single digit.

Step 8: If i is composite and the single digit sum is 1, display i and increment c.

Step 9: Use c to decide comma placement while printing the list.

Step 10: After the loop ends, display the frequency c.

Step 11: Stop.

Explanation:

The program searches a range for numbers that satisfy two conditions: the number must be composite, and it must also be a magic number. The solution uses helper methods so that each condition is checked separately. This makes the main loop easier to read because it only needs to call isComposite() and isMagic() for every number in the range.

The method isComposite() counts the factors of a number. It tests every value from 1 to the number and increases count whenever divisibility is exact. If the factor count is more than 2, the number is composite. This works because a prime number has exactly two factors, while a composite number has more.

The method sumDig() adds the digits of a number using repeated extraction with % 10 and removal with / 10. The method isMagic() first finds the digit sum and then repeats digit summing while the result is still more than one digit. A number is magic when this repeated digit sum finally becomes 1. In the main range loop, a number is printed only when both helper methods return true. The counter c also controls comma placement and records the final frequency.

The separation into three methods also makes the program easier to test. If a result is wrong, the composite check, digit-sum calculation and magic check can be traced independently before examining the range-printing logic.

Java Program:

Java
/**
* The class MagicComposite_ISC2014 inputs two integers and prints all those numbers
* which are composite as well as Magic
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2014 Question 1
*/

import java.util.Scanner;
class MagicComposite_ISC2014
{
    boolean isComposite(int n) // Function to check for Composite number
    {
        int count=0;
        for(int i=1;i<=n;i++)
        {
            if(n%i==0)
            count++;
        }
        if(count>2)
        return true;
        else
        return false;
    }

    int sumDig(int n) // Function to return sum of digits of a number
    {
        int s = 0;
        while(n>0)
        {
            s = s + n%10;
            n = n/10;
        }
        return s;
    }

    boolean isMagic(int n) // Function to check for Magic number
    {
        int a = sumDig(n);
        while(a>9)
        {
            a = sumDig(a);
        }

        if(a == 1)
        return true;
        else
        return false;
    }

    public static void main(String args[])
    {
        MagicComposite_ISC2014 ob = new MagicComposite_ISC2014();
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the lower limit(m) : ");
        int m=sc.nextInt();
        System.out.print("Enter the upper limit(n) : ");
        int n=sc.nextInt();

        int c=0;
        if (m<n)
        {
            System.out.println("The Composite Magic Integers are: ");
            for(int i=m; i<=n; i++)
            {
                if(ob.isComposite(i)==true && ob.isMagic(i)==true)
                {
                    if (c==0) // Printing the first number without any comma
                    System.out.print(i);
                    else
                    System.out.print(", "+i);
                    c++;
                }
            }
            System.out.println("\nThe frequency of Composite Magic Integers is : "+c);
        }

        else
        System.out.println("INVALID INPUT");
    }
}

Equivalent Python Program:

Python
# Read the required input values for the program.
# Helper functions keep repeated calculations separate from the main logic.
# Process the data using loops, conditions and helper logic.
# Display the final result in the required format.

def is_composite(n):
    count = 0
    for i in range(1, n + 1):
        if n % i == 0:
            count = count + 1
    return count > 2

def sum_digits(n):
    total = 0
    while n > 0:
        total = total + n % 10
        n = n // 10
    return total

def is_magic(n):
    s = sum_digits(n)
    while s > 9:
        s = sum_digits(s)
    return s == 1

m = int(input("m = "))
n = int(input("n = "))

if m >= n:
    print("INVALID INPUT")
else:
    count = 0
    print("THE COMPOSITE MAGIC INTEGERS ARE:")
    for i in range(m, n + 1):
        if is_composite(i) and is_magic(i):
            if count == 0:
                print(i, end="")
            else:
                print(", " + str(i), end="")
            count = count + 1
    print()
    print("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS:", count)

Output:

Example 1 INPUT: m = 10 n = 100 OUTPUT: THE COMPOSITE MAGIC INTEGERS ARE: 10, 28, 46, 55, 64, 82, 91, 100 FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8 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 Example 3 INPUT: m = 120 n = 99 OUTPUT: INVALID INPUT

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →