Free2Code
 
Time: 2008-12-04, 07:44pm
checking cube of digits
Subject: checking cube of digits  ·  Posted: 2006-09-15, 08:43pm
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!!!!

 
  Reply to this ·  Post link ·  Top
Subject: Re: checking cube of digits  ·  Posted: 2006-09-15, 10:21pm
Rank: ? (119)
Member #: 28292
Try this:
Code:
  1. int x = 153 //The number is here
  2. if(x < 1 || x > 1000)
  3. return;
  4. int digits[] = new int[20];
  5. // Making of digits
  6. for(int i = 0, y = x; x != 0; i++, x /= 10)
  7. digits[i] = x % 10;
  8. // the sum of its digits to the power of 3.... 
  9. for(int j = 0, sum = 0; j < i; j++)
  10. sum += Math.pow(digits[j], 3);
  11. if(sum == y)
  12. {
  13. // Input is correct
  14. }
  15. else
  16. {
  17. // Input is not correct
  18. }


» Post edited 2006-09-16, 02:38am by igorok.

 
  Reply to this ·  Post link ·  Top
Subject: Re: checking cube of digits  ·  Posted: 2006-09-16, 01:02am
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];



 
  Reply to this ·  Post link ·  Top
Subject: Re: checking cube of digits  ·  Posted: 2006-09-16, 02:19am
Rank: ? (119)
Member #: 28292
Look the code above. I have some corrected.

» Post edited 2006-09-16, 02:37am by igorok.

 
  Reply to this ·  Post link ·  Top
This post has been removed by the author
 
  Top
Subject: Re: checking cube of digits  ·  Posted: 2006-09-19, 11:00pm
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.";

}
}
}


 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons