Dudeney Number in Java
What is a Dudeney Number?
A dudeney number is a positive integer such that it's cube root equals its sum of digits.
Some examples of Dudeney Numbers
Consider the number 512.
Sum of digits = 5 + 1 + 2 = 8.
Cube root of 512 = 8.
So, 512 is a dudeney number.
Consider another number 4913.
Sum of digits = 4 + 9 + 1 + 3 = 17.
Cube root of 4913 = 17.
So, 4913 is also a dudeney number.
Dudeney Number Program in Java
import java.util.Scanner;
class Dudeney{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = Integer.parseInt(in.nextLine());
int sum = 0;
for(int i = num; i != 0; i /= 10)
sum += i % 10;
double c = Math.cbrt(num);
if(sum == c)
System.out.println(num + " is a dudeney number");
else
System.out.println(num + " is not a dudeney number");
}
}
Comments
Post a Comment