Sunday, 21 July 2024

Dudeney Number - Java

A Dudeney number is a positive integer for which the sum of its digits is equal to the the cube root of the number itself.

For example - 512

5+1+2=8

83 = 512

Here is the java code for determining if a number is Dudeney or not.

import java.util.Scanner;
class Dudeney
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number");
        int n = sc.nextInt();
        int sum = 0,temp = n, cube = 0;
        while(temp!=0)
        {
            sum+=temp%10;
            temp/=10;
        }
        cube = sum*sum*sum;
        if(cube == n)
        {
            System.out.println("It is a Dudeney number");
        }
        else
        {
            System.out.println("It is not a Dudeney number");
        }
    }
}

Demonstration:

1. First, Scanner class is imported.
2. A class namely Dudeney have been defined.
3. A main method have been defined.
4. Object of Scanner class have been created.
5. A Number has been demanded from user by giving a message.
6. To accept and store the number, a variable namely n has been defined of int data type
7. Then, three different variables namely
    a) sum - First, the individual digits will be extracted due to the presence of temp%10. Then, these individual digits shall get added to sum.
    b) temp - This variable will hold the number given by user on temporary basis. Actually, entire operation will be done with temp variable. temp = n means the value stored in n namely variable is getting assigned to temp. It means that throughout the entire operation, original value stored in n namely variable will remain safe.
    c) cube - This variable will hold the cube of the number that is stored in sum.

Now, let us see the working:

while(temp!=0)

This loop will continue its iteration until value of temp doesn't become equals to 0. Say for example, user gave 512. Here, first 2 will be extracted, then 1 and at last, 5 will get extracted as it is in index 0. Refer to the below diagram for clear understanding.



sum+ =temp%10;

Here, we need to understand few things.

First, individual digits from given number is getting extracted using temp%10 and getting stored in sum.
As in case of 512, the first extracted number using temp%10 will be 2.
Similarly, second number using temp%10 will be 1 and it will also be added to sum.
At last, third number using temp%10 will be 5 and it will also be added to sum.

N
ow, sum is containing 2+1+5 = 8

temp/=10;

In previous operation, we have seen that the first extracted number using temp%10 was 2. Immediately, after this operation, temp/=10; will be executed which means 512 is now going to be divided by 10 resulting to 51.2. This operation will permanently delete this 2 from temp variable. so that in next iteration, 2 doesn't gets added to the sum variable again. Like this, it will keep on deleting the digits from temp variable.


Here is the final outcome:

sum is now containing sum of digits i.e. 5+1+2 = 8
n variable is containing 512

cube = sum*sum*sum;

This operation outside the while loop will find the product of sum variable and that product will be assigned to cube.

Now, cube is containing 512

Finally, with the conditional statement using if-else, it is determined if cube is equal to n or not as it can be seen in the program. If cube is equal to n, then it is displaying that the number is a Dudeney number. Otherwise, it is displaying that the number is not a Dudeney number.

Now, write this program and test it with various numbers and increase your knowledge.

Happy Coding.......


























1 comment: