Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 6(i)

8 marks

Computer ScienceISC 2026 Improvement Computer Science Paper 1ProgramObjects

Write the program described below.

Design a class Powerful to check if a given number is powerful or not. A Powerful number is a…

Design a class Powerful to check if a given number is powerful or not. A Powerful number is a number which is equal to the sum of its digits, raised to the power of the digit itself. Example: $3435 = 3^3 + 4^4 + 3^3 + 5^5 = 27 + 256 + 27 + 3125 = 3435$ The details of the members of the class are given below:
Class name:Powerful
Data member/instance variable:
num:to store a positive integer
Methods/Member functions:
Powerful( ):constructor to initialise the data member with legal initial value
void input( ):to accept a positive integer
int sum(int num):to return the sum of the $\text{digit}^{\text{digit}}$ for all digits of num using recursive technique
void check( ):to check whether the given number is a powerful number by invoking the function sum( ) and display the result with an appropriate message
Specify the class Powerful giving details of the constructor( ), void input( ), int sum(int) and void check( ). Define the main( ) function to create an object and call the functions accordingly to enable the task.

Answer

Answer

import java.util.Scanner;

class Powerful
{
    long num;

    Powerful()
    {
        num = 0;
    }

    void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        num = sc.nextLong();
    }

    int sum(int num)
    {
        if (num == 0)
            return 0;
        int digit = num % 10;
        return (int) Math.pow(digit, digit) + sum(num / 10);
    }

    void check()
    {
        int s = sum((int) num);
        if (s == num)
            System.out.println(num + " is a Powerful number.");
        else
            System.out.println(num + " is not a Powerful number.");
    }

    public static void main(String[] args)
    {
        Powerful obj = new Powerful();
        obj.input();
        obj.check();
    }
}
Explanation: input() reads the number. sum(num) recursively adds digit^digit for the last digit of num and the sum for the remaining digits (num/10), stopping when num becomes 0. check() compares this digit-power sum to the original number and prints the appropriate message. Tested (run for real): for num = 3435, sum(3435) = 3^3+4^4+3^3+5^5 = 3435, and the program correctly printed "3435 is a Powerful number."