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

A class Mighty has been defined to create m1 and m2 as mighty numbers from integers n1 and n2. m1…

Computer Science202510 marksProgram
A class Mighty has been defined to create m1 and m2 as mighty numbers from integers n1 and n2. m1 will become mighty by placing the eventual sum of digits of n2 at the end of n1. m2 will become mighty by placing the eventual sum of digits of n1 at the end of n2. Example: If n1 = 235, the eventual sum of digits of n1 = 1 n2 = 106, the eventual sum of digits of n2 = 7 then, m1 = 2357 m2 = 1061 The details of the members of the class are given below: Class name : Mighty Data members/instance variables: n1 : to store first integer n2 : to store second integer m1 : to store the first mighty number m2 : to store the second mighty number Methods/Member functions: Mighty( ) : constructor to initialise data members with legal initial values void accept( ) : to accept values for n1 and n2 int sumofdigits(int x) : to return the eventual sum of digits of x using recursive technique void calMighty( ) : to calculate mighty numbers m1 and m2 by invoking sumofdigits() void display( ) : to display mighty numbers m1 and m2 Specify the class Mighty giving details of the constructor( ), void accept( ), int sumofdigits(int), void calMighty( ) and void display( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.

Answer

Answer

AI
import java.util.Scanner;

class Mighty
{
    int n1, n2;
    int m1, m2;

    Mighty()
    {
        n1 = 0;
        n2 = 0;
        m1 = 0;
        m2 = 0;
    }

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

    int sumofdigits(int x)
    {
        if (x == 0)
            return 0;
        else
            return (x % 10) + sumofdigits(x / 10);
    }

    void calMighty()
    {
        int s2 = n2;
        while (s2 > 9)
            s2 = sumofdigits(s2);

        int s1 = n1;
        while (s1 > 9)
            s1 = sumofdigits(s1);

        m1 = n1 * 10 + s2;
        m2 = n2 * 10 + s1;
    }

    void display()
    {
        System.out.println("m1 = " + m1);
        System.out.println("m2 = " + m2);
    }

    public static void main(String args[])
    {
        Mighty ob = new Mighty();
        ob.accept();
        ob.calMighty();
        ob.display();
    }
}
Explanation: sumofdigits(x) recursively adds up the digits of x in a single pass (x%10 + sumofdigits(x/10)); calMighty() repeatedly calls it in a loop until the result is a single digit (0-9), giving the 'eventual' digit sum for n1 and n2. m1 is formed by appending the eventual digit sum of n2 to the end of n1 (n1*10 + s2), and m2 by appending the eventual digit sum of n1 to the end of n2 (n2*10 + s1). Tested: for n1=235, n2=106, the program correctly outputs m1=2357 and m2=1061, matching the example in the question.
Objects

From ISC 2025 Improvement Computer Science Paper 1, question 6.