PRASHNIKAप्रश्निका
Back to the paper

A class LCM has been defined to find the Lowest Common Multiple of two integers. Some of the data…

Computer Science202510 marksProgram
A class LCM has been defined to find the Lowest Common Multiple of two integers. Some of the data members and member functions are given below: Class name : LCM Data members/instance variables: n1 : to store an integer number n2 : to store an integer number large : integer to store the largest from n1,n2 sm : integer to store the smallest from n1,n2 l : to store lcm of two numbers Methods / Member functions: LCM( ) : default constructor to initialize data members with legal initial values void accept( ) : to accept n1 and n2 int getLCM( ) : returns the lcm of n1 and n2 using the recursive technique void display( ) : to print the numbers n1, n2 and lcm Specify the class LCM giving details of the constructor( ), void accept( ), int getLCM() and void display( ). Define a main ( ) function to create an object and call the member functions accordingly to enable the task.

Answer

Answer

Official answer key
import java.util.Scanner;

class LCM
{
    int n1, n2;
    int large, sm;
    int l;

    LCM()
    {
        n1 = 0; n2 = 0;
        large = 0; sm = 0;
        l = 0;
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first number  : ");
        n1 = sc.nextInt();
        System.out.print("Enter second number : ");
        n2 = sc.nextInt();
        large = (n1 > n2) ? n1 : n2;
        sm = (n1 > n2) ? n2 : n1;
    }

    int getLCM()
    {
        if (large % sm == 0)
            return (n1 * n2) / sm;
        else
        {
            int temp = large;
            large = sm;
            sm = temp % sm;
            return getLCM();
        }
    }

    void display()
    {
        l = getLCM();
        System.out.println("N1  = " + n1);
        System.out.println("N2  = " + n2);
        System.out.println("LCM = " + l);
    }

    public static void main(String args[])
    {
        LCM ob = new LCM();
        ob.accept();
        ob.display();
    }
}
Explanation: getLCM() recursively computes the GCD of large and sm using the Euclidean algorithm (large % sm == 0 is the base case; otherwise it sets large=sm, sm=large%sm and recurses), then applies the relation LCM = (n1*n2)/GCD. Tested for n1=12, n2=18: the program correctly outputs LCM=36 (verified by running the code).
Recursion

From ISC 2025 Specimen Computer Science Paper 1, question 8.