-1

I was working on homework assignments about making a program that allows the user to enter in a low and high value range and the program was suppose to find all the numbers that were sum of their cubed digits (IE 153 = 1^3 + 5^3 + 3^3) and a for loop has to be used. I'm stuck on figuring out how to go about this, I obviously need to separate each digit of the number take each digit cube it, add them together then check to see if it equals the value if not repeat but that's as far as I have gotten planning wise help would be appreciated.

/* 
*   problem #17 on page 367
*   Program Description - finding numbers that equal the sum of their cubed numbers
*   Author: Jonathan Wilson
*   Date:   2/24/2015
*   Filename:   cube.java
*/


//import statements
import java.util.*;     //for scanner class 


// class beginning
class Cube {
    public static void main(String[] args ) { 
        //Declare variables area

        int low;
        int high;
        int value;
        Scanner scan = new Scanner (System.in); 


        //Program beginning messages to user here
        System.out.println("Welcome to my special number program!");
        System.out.println("Enter a low and high value range");
        System.out.println("and this program will find all the numbers that equal the sum of their cubed digits");

        //Collect inputs from user or read in data here


        System.out.println("Enter in the low starting range ");
        low=scan.nextInt();  //storing low range as low
        System.out.println();  //break
        System.out.println("Enter in the high ending range ");
        high=scan.nextInt();  //storing the high range as high
        System.out.println();  //break


        // A check to see that the range entered is a legal range, if so then it moves on to print out range and goes
        // into main code

        while (high < low){//checks to see if the inputs are valid and that the user doesn't try to do an inverse range
            System.out.println();
            System.out.println("Incorrect Range try again!");
            System.out.
            low = scan.nextInt();
            System.out.print(Low);
            System.out.println();//break
            System.out.print("Please enter the higher number in the range... ");
            high = scan.nextInt();

            //Echo input values back to user here
            System.out.println("Okay, so the range of numbers to look through are "+Low+ " to "+High+" lets start!");

            // Main code and calculations to do
            for (count= low; count <= high; count ++){


                //Output results here




                //End program message
                System.out.println();
                System.out.println("Hope you enjoyed using this program!");
            }// end main method 

        }
    }   
}// end class
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • You could take a look at [this](http://stackoverflow.com/questions/3389264/how-to-get-the-separate-digits-of-an-int-number) which allows you to get the individual digits of a number. – MadProgrammer Mar 17 '15 at 03:48
  • Google "extract digit from number java" to know how to get each digit from a number. – Spikatrix Mar 17 '15 at 03:50
  • Can you give a more meaningful title? And unless your story have any importance on the question, I do not think anyone is interested to know. Keep your question clear and concise please. btw, it is just unbelievable for a people knows Python to have source code that poorly indented... :( – Adrian Shum Mar 17 '15 at 04:36
  • Found that your program will not even compile when I am cleaning up your question... Can you be more careful when you are trying to ask help from other? – Adrian Shum Mar 17 '15 at 05:58

1 Answers1

0

You can isolate digits by using the % or mod tool. Then, to go to the next digit, you would divide the number by 10, thus making the last digit go away. This process can be used to go through the digits of a number.

//import statements
import java.util.*; //for scanner class 
// class beginning
public class Test {
    public static void main(String[] args) {
        // Declare variables area

        int low;
        int high;
        int value;
        Scanner scan = new Scanner(System.in);

        // Program beginning messages to user here
        System.out.println("Welcome to my special number program!");
        System.out.println("Enter a low and high value range");
        System.out
                .println("and this program will find all the numbers that equal the sum of their cubed digits");

        // Collect inputs from user or read in data here

        System.out.println("Enter in the low starting range ");
        low = scan.nextInt();// storing low range as low
        System.out.println();// break
        System.out.println("Enter in the high ending range ");
        high = scan.nextInt();// storing the high range as high
        System.out.println();// break

        // A check to see that the range entered is a legal range, if so then it
        // moves on to print out range and goes
        // into main code

        while (high < low) {// checks to see if the inputs are valid and that
                            // the user doesn't try to do an inverse range
            System.out.println();
            System.out.println("Incorrect Range try again!");
            low = scan.nextInt();
            System.out.print(low);
            System.out.println();// break
            System.out.print("Please enter the higher number in the range... ");
            high = scan.nextInt();
        }
        // Echo input values back to user here
        System.out.println("Okay, so the range of numbers to look through are "
                + low + " to " + high + " lets start!");

        // Main code and calculations to do
        for (int count = low; count <= high; count++) {
            int sum=0;
            int original = count;
            while(count>0){
                sum+=(count%10)*(count%10)*(count%10);
                count/=10;
            }
            if(sum==original){
                System.out.println(original+" works as a number!");
            }
            count = original;
        }


        // Output results here

        // End program message
        System.out.println();
        System.out.println("Hope you enjoyed using this program!");
    }// end main method
}// end class