ISC Practicals and Theory - Dudeney Number
Updated 21 Oct 2026
A Dudeney number is a positive whole number that is a perfect cube, and whose digits add up to the cube root of the number. This note gives the program written in the main method alone: it adds the digits, cubes the sum and compares it with the number.
Definition
A number $N$ is a Dudeney number if $N$ is a perfect cube and the sum of the digits of $N$ is equal to the cube root of $N$. In other words, if $s$ is the sum of the digits of $N$, then $s^3 = N$.
- $512$ is a Dudeney number: $512 = 8^3$ and $5 + 1 + 2 = 8$.
- $4913$ is a Dudeney number: $4913 = 17^3$ and $4 + 9 + 1 + 3 = 17$.
- $125$ is not a Dudeney number: $125 = 5^3$, but $1 + 2 + 5 = 8$ and $8^3 = 512$, which is not $125$.
- $1$ is a Dudeney number: $1 = 1^3$ and the sum of its digits is $1$.
Algorithm
- Accept the number
num. Ifnum <= 0, displayInvalid inputand stop. - Copy
numintotempand setsum = 0. - While
temp > 0: take the last digitd = temp % 10, add it tosumand remove it withtemp = temp / 10. - Find
cube = sum * sum * sum. Thelongtype is used so that the cube does not overflow. - If
cube == num, display thatnumis a Dudeney number. Otherwise display that it is not.
Program
Everything is written inside main: the input, the loop that adds the digits, the cube and the comparison.
import java.util.Scanner;
public class Dudeney
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt(); // the number to be checked
if (num <= 0)
{
System.out.println("Invalid input");
}
else
{
int temp = num; // copy of num whose digits are added
int sum = 0; // sum of the digits
while (temp > 0)
{
int d = temp % 10; // last digit
sum += d;
temp /= 10;
}
long cube = (long) sum * sum * sum; // cube of the sum of the digits
if (cube == num)
System.out.println(num + " is a Dudeney number");
else
System.out.println(num + " is not a Dudeney number");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
num | int | The number entered by the user |
temp | int | Copy of num whose last digit is removed in each pass |
sum | int | Sum of the digits of num |
d | int | The last digit of temp in the current pass |
cube | long | The cube of sum |
Sample input and output
Sample 1
Enter a number: 512
512 is a Dudeney numberSample 2
Enter a number: 125
125 is not a Dudeney numberSample 3
Enter a number: 1
1 is a Dudeney numberSample 4
Enter a number: -5
Invalid inputDry run
Input: num = 512
First the digits are added, starting with temp = 512 and sum = 0:
| Pass | d = temp % 10 | sum = sum + d | temp = temp / 10 |
|---|---|---|---|
| Start | - | 0 | 512 |
| 1 | 2 | 2 | 51 |
| 2 | 1 | 3 | 5 |
| 3 | 5 | 8 | 0 |
The loop stops because temp = 0, and sum = 8.
Then cube = 8 * 8 * 8 = 512. As cube == num is true, the program shows that $512$ is a Dudeney number, as in the first sample run.
Variations
- Range: "Display all Dudeney numbers between m and n."
- Recursive technique: "Check whether a number is a Dudeney number, using a recursive method to find the sum of the digits."
- Perfect cube: "Check whether a number is a perfect cube."
- Sum of digits: "Find the sum of the digits of a number, using a separate method."