Write your name or username here: _______________________

Do this when you receive the paper.

Write a program that checks to see if a number is equal to the sum of the cubes of its digits. The input is a String (a line of text entered by the user). Feel free to use ConsoleReader to read lines from the user.

Here's how your program might work:

frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
Type> 102
No, the sum of the cubes is 9 and the number is 102 so they're not equal.
Type> 153
Yes, the sum of the cubes of the digits is equal to the number itself for 153
Type> 189
No, the sum of the cubes is 1242 and the number is 189 so they're not equal.
Type> done
frilled.cs.indiana.edu%
Use the lab time to develop the program. You can use any notes or books you have including web notes. You are not allowed to communicate with anyone except the AIs. Ask questions if something is unclear to you and they will try to clarify it as much as they can. The can't and won't help you with the actual development of the program but they might be able to help to clarify anything that might result in a misunderstanding on your part.

When you're done turn the program in OnCourse under the Dropbox for the time and location of the exam even if you're from another section. Then write the program by hand on the back of this page and return it to the AI before you leave. I will grade this paper but I might refer to your posted code in OnCourse as well so please do both.

You can if you want turn in additional materials such as: pseudocode, flow charts, additional explanations of the code and/or the approach chosen. Be sure to solve as much as you can and to turn in a program in OnCourse at the end of the lab in addition to turning in the paper with your exam.

Good luck and do well.

class One {
    public static void main(String[] args) {
	ConsoleReader c = new ConsoleReader(System.in); 
	String line; 
	System.out.print("Type> "); 
	line = c.readLine(); 
	while (! line.equals("done")) {
	    int number = 0; 
	    int sum = 0; 
	    for (int i = 0; i < line.length(); i++) {
		int digit = Integer.parseInt(line.substring(i, i+1)); 
                sum += digit * digit * digit; 
	    }
	    if (sum == Integer.parseInt(line)) {
		System.out.println("Yes, the sum of the cubes of the digits is equal to the number itself for " + Integer.parseInt(line)); 
	    } else { 
		System.out.println("No, the sum of the cubes is " + sum + " and the number is " + Integer.parseInt(line) + " so they're not equal."); 
	    } 
	    System.out.print("Type> ");
	    line = c.readLine();
	}
    }
}