In: Computer Science
Create a CubesSum application that prompts the user for a non-negative integer and then displays the sum of the cubes of the digits.
b) Modify the application to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits.(Java Programming )
import java.util.*;
class Hello {
public static int solve(int n) {
// initially the sum = 0
int total = 0;
while (n > 0) {
// taking the cube of the digit and adding to the sum
total += (n % 10) * (n % 10) * (n % 10);
n = n / 10;
}
// returing the sum
return total;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(solve(n));
// for 2,3,4 digit numbers
for (int i = 10; i <= 9999; i++) {
// if their sum of cubes is equal to the number then printing
if (i == solve(i)) {
System.out.println("The sum of the cubles of the " + i + " and " + i + " are same");
}
}
}
}
output: