Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Accept the number num. If num <= 0, display Invalid input and stop.
  2. Copy num into temp and set sum = 0.
  3. While temp > 0: take the last digit d = temp % 10, add it to sum and remove it with temp = temp / 10.
  4. Find cube = sum * sum * sum. The long type is used so that the cube does not overflow.
  5. If cube == num, display that num is 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
numintThe number entered by the user
tempintCopy of num whose last digit is removed in each pass
sumintSum of the digits of num
dintThe last digit of temp in the current pass
cubelongThe cube of sum

Sample input and output

Sample 1

Enter a number: 512
512 is a Dudeney number

Sample 2

Enter a number: 125
125 is not a Dudeney number

Sample 3

Enter a number: 1
1 is a Dudeney number

Sample 4

Enter a number: -5
Invalid input

One value per line

Dry run

Input: num = 512

First the digits are added, starting with temp = 512 and sum = 0:

Passd = temp % 10sum = sum + dtemp = temp / 10
Start-0512
12251
2135
3580

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

  1. Range: "Display all Dudeney numbers between m and n."
  2. Recursive technique: "Check whether a number is a Dudeney number, using a recursive method to find the sum of the digits."
  3. Perfect cube: "Check whether a number is a perfect cube."
  4. Sum of digits: "Find the sum of the digits of a number, using a separate method."