0
Find the sum of the cubes of the digits of no. 153
what will be the program for this?
2 Answers
+ 2
There are some java classes that handle complex mathematical calculation
BigDecimal
BigInteger
MathContex;
https://docs.oracle.com/javase/7/docs/api/
But you can use Math class that is part of java.lang; so you don't need to import anything.
There is a method called pow() and as parameters it takes two double values the first element is the base of the number and the secon element is the exponent of the base below I will post the code to calculate the cubic sum of this number 153 Hope this is the answer you are looking for.
public class CubicSum{
public static void main(String [] args){
//this is the method
Math.pow(135,3);
//let's print it out
System.out.println(Math.pow(135,3));
}
}
//keep in mind this method has more functions that this hope you understand
+ 1
use string and the charAt methods
solving this problem will be a some thing like that
Scanner in =new Scanner(System.in);
String n=in.next();
long number,sum=0;
for(int i =0;i<n.length();i++)
{
number=Long.parseLong(n.charAt(i)+"");
sum+=Math.pow(number,3);
}
System.out.println(sum);