Question 4

A Dudeney number is a positive integer that is a perfect cube and also such that the sum of its digits is equal to the cube root of the number. Write a program in Java to input a number, check, and print whether it is a Dudeney number or not.

Example:

Consider the number 512.

Sum of digits \(= 5 + 1 + 2 = 8\)

Cube root of \(512 = 8\)

As the sum of digits [tex]\(= \)[/tex] cube root of the number, hence 512 is a Dudeney number.



Answer :

To determine if a number is a Dudeney number, we need to perform two main operations: check if the number is a perfect cube, and verify that the sum of its digits equals the cube root of the number. Below is a detailed, step-by-step solution for this problem:

1. Input the number: We start by receiving the number to be checked.

2. Find the cube root:
- Calculate the cube root of the number. This can be done using the mathematical operation of taking the cube root, which is the number raised to the power of 1/3.
- For a number to be a perfect cube, the cube root when rounded should, when cubed again, return the original number.

3. Check if the number is a perfect cube:
- Cube the rounded cube root.
- Compare it to the original number. If they match, the number is a perfect cube.

4. Sum the digits of the number:
- Convert the number to a string to easily iterate through each digit.
- Convert each character back to an integer and sum them up.

5. Compare the sum of digits to the cube root:
- If the sum of digits equals the cube root, we conclude that the number is a Dudeney number.

### Example
Let's consider the number 512:
- The cube root of \( 512 \) is \( 8 \) (because \( 8^3 = 512 \)).
- The sum of the digits of \( 512 \) is \( 5 + 1 + 2 = 8 \).
- Since \( 8 \) (sum of digits) equals \( 8 \) (cube root), \( 512 \) is confirmed to be a Dudeney number.

### Code Implementation in Java
Here is how you can implement this logic in Java:

```java
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();

if (isDudeney(num)) {
System.out.println(num + " is a Dudeney number.");
} else {
System.out.println(num + " is not a Dudeney number.");
}
}

public static boolean isDudeney(int num) {
// Calculate the cube root and round it
int cubeRoot = (int) Math.round(Math.pow(num, 1.0/3.0));

// Check if the number is a perfect cube
if (Math.pow(cubeRoot, 3) != num) {
return false;
}

// Calculate the sum of the digits of the number
int sumOfDigits = 0;
int temp = num;
while (temp > 0) {
sumOfDigits += temp % 10;
temp /= 10;
}

// Check if the sum of digits equals the cube root
return sumOfDigits == cubeRoot;
}
}
```

### Explanation of the Code
- Input Handling: The `Scanner` class is used to take input from the user.
- Cube Root Calculation: We use `Math.pow(num, 1.0/3.0)` to find the cube root of the number and cast it to an integer after rounding.
- Checking Perfect Cube: We verify if cubing the cube root gives us back the original number.
- Sum of Digits Calculation: We iterate through each digit of the number, summing them up.
- Comparison: Finally, we check if the sum of the digits is equal to the cube root and return the result.

This solution will correctly identify if a number is a Dudeney number by following the defined steps.