checking cube of digits
|
|||
|
Rank: ? (5)
Member #: 28493 |
Hey, guys... anyone knows how to write a code to ask the user to input a value between [1,1000] inclusively, so that we can check whether the number entered is the sum of its digits to the power of 3....
For example: 153 = 1^3 + 2^3 + 3^3 , so this input is correct; But if the user type in 783 = 7^3 + 8^3 +3^3 , after checking, this is not a correct input... Help!!!! |
||
|
|||
|
|||
|
Rank: ? (119)
Member #: 28292 |
Try this:
Code:
» Post edited 2006-09-16, 02:38am by igorok.
|
||
|
|||
|
|||
|
Rank: ? (5)
Member #: 28493 |
Hey, I follow ur instruction, but how come after compiling, an error shown.....
May U help me 2 check?? Thx.... import java.util.*; class MagicCube { public static void main (String [] args){ int x = 153 ; //The number is here if(x < 1 || x > 1000) { return; } int digits [15]; // Making of digits for(int i = 0, y = x; x != 0; i++, x /= 10) { digits[i] = x % 10; } // the sum of its digits to the power of 3.... for(int j = 0, sum = 0; j < i; j++) { sum += Math.pow(digits[i], 3); } if(sum == y) { // Input is correct } else { // Input is not correct } } } 1 error found: File: .... [line: 11] Error: ']' expected[b]int digits [15]; |
||
|
|||
|
|||
|
Rank: ? (119)
Member #: 28292 |
|||
|
|||
|
This post has been removed by the author
|
|||
|
|||
|
|||
|
Rank: ? (5)
Member #: 28493 |
If my code that this, how come the output is always "... is the sum of the cube of is digits"..
Eg: If the input is 153, the output will be"15 is the some of the cube of its digits." If the input is 3333, the output will be "333 is the some of its digits." That's weird......I think there must be sth wrong with my code, anyone can check for me?? import java.util.*; class CubeDigits { public static void main (String[] args) { int n=0; ; int num, sum = 0, remainder; Scanner scanner = new Scanner(System.in); System.out.print("Enter value: " ); num = scanner.nextInt(); System.out.println("" if(num > 0 || sum==num){ remainder = num % 10; sum += Math.pow(remainder, 3); num = num / 10; n = num; System.out.println(n + " is the sum of the cube of its digits." } else { System.out.println(n + " is not the sum of the cube of its digits." } } } |
||
|
Please login or register to post a reply.