Advertisements
Advertisements
Question
Write a program to enter a number and print the cube of each digit. Print the sum of the cube of the digits.
Code Writing
Advertisements
Solution
import java.util.*;
class clDigits
{
void main()
{
Scanner sc = new Scanner(System.in);
int N, d, dq, sum = 0;
System.out.print("Enter a number : ");
N = sc.nextInt();
int cn = N;
while(cn > 0)
{
d = cn % 10;
dq = d * d * d;
System.out.println("cube of digit " + d + ":" + dq);
sum = sum + dq;
cn = cn / 10;
}
System.out.println("Sum of cube of digits of " + N + ":" + sum);
}
}
Output:
Enter a number : 742
cube of digit 2:8
cube of digit 4: 64
cube of digit 7: 343
Sum of cube of digits of 742 : 415
shaalaa.com
Is there an error in this question or solution?
