Skip to content

1.10 Integer is cubic sum or not

Farrukh Ahmad edited this page Dec 18, 2015 · 1 revision

Here program need to check simple number whether that is cubic sum or not

For example 153 = (1x1x1 ) + (5x5x5) + (3x3x3)

public class CubicSum {

public static void main(String[] args) {
	int n = 371;
	
	System.out.println(isCubicSum(n));
			
			
}

static boolean isCubicSum(int n){
	
	int original = n;
	int sum = 0;
	
	while(n!=0){
		int reminder = n % 10;
		sum = sum + (reminder*reminder*reminder);
		n = n / 10;
	}
	return original == sum;
}

}

Clone this wiki locally