I wrote a simple program in java for finding out whether a number is a dudeney number or not :
import java.util.Scanner ;
public class dudeney_number
{
public static void main()
{
Scanner sc = new Scanner(System.in) ;
System.out.print("Enter a number : ") ;
int n = sc.nextInt() ;
int temp = n ;
int sum = 0 ;
while(temp!=0)
{
sum+=temp%10 ;
temp/=10 ;
}
if(sum==Math.cbrt(n))
{
System.out.println("Dudeney number") ;
} else {
System.out.println("Not a dudeney number") ;
}
}
}
I create a Scanner object (sc
), input a number from the user (n
) and find out the sum of the digits in the entered number (sum
). I then check for equality between the sum of the digits and the cube root of the number entered using Math.cbrt(n)
.
sum
is an integer value with datatype int
. Math.cbrt(n)
returns a double
value.
Why does the compiler not return an error when a 32-bit and a 64-bit value is compared ?