Dudeney Number Java Program | ISC Computer Science Paper 1

Design a class NumDude to check if a given number is a Dudeney number or not. A Dudeney number is a positive integer that is a perfect cube, such that the sum of its digits is equal to the cube root of the number.

Example: 5832 = (5 + 8 + 3 + 2)3 = 183 = 5832

Some of the members of the class are given below:

Class name: NumDude
Data member/instance variable:
num: to store a positive integer number
Methods/Member functions:
NumDude(): default constructor to initialize the data member with legal initial value.
void input(): to accept a positive integer.
int sumDigits(int x): returns the sum of the digits of number ‘x’ using recursive technique.
void isDude(): checks whether the given number is a Dudeney number by invoking the function sumDigits() and display the result with an appropriate message.

Specify the class NumDude giving details of the constructor(), void input(), int sumDigits(int) and void isDude(). Define a main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
class NumDude{
    private int num;
    public NumDude(){
        num = 0;
    }
    public void input(){
        Scanner in = new Scanner(System.in);
        System.out.print("POSITIVE INTEGER: ");
        num = Integer.parseInt(in.nextLine());
        num = Math.abs(num);
    }
    public int sumDigits(int x){
        if(x < 10)
            return x;
        return x % 10 + sumDigits(x / 10);
    }
    public void isDude(){
        if(Math.cbrt(num) == sumDigits(num))
            System.out.println("DUDENEY NUMBER");
        else
            System.out.println("NOT A DUDENEY NUMBER");
    }
    public static void main(String[] args){
        NumDude obj = new NumDude();
        obj.input();
        obj.isDude();
    }
}