Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Create a java program to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a java program to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits?

    import java.util.*;
    import java.io.*;
     
    class Cubesum {
    	public static void main(String args[]){
    		int input=0;
    		int num1,num2,num3;
     
    		//read the number
    		System.out.print("Enter a positive integer: ");
    		Scanner console = new Scanner(System.in);
    		input= Integer.parseInt(console.nextLine());
     
    		int number = input; //number is a temp variable
    		int counter = 0; //counter is used to count no of digits
     
    		while(number>0){
    			int t= number%10;
    			counter += t * t * t;
    			number = number/10;
    		}
    		System.out.println("The sum of the cubes of the digits is: "+counter);
     
    	}
    }
    Output:
    Enter a positive integer: 223
    The sum of the cubes of the digits is: 43

    I want to get this output from the program:
    371 = 3³+7³+1³
    How would I do it?


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create a java program to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits?

    You have the wrong formula.
    Hint: you need to use the math class.
    Another hint: Math.pow(num1, 3) //example

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Create a java program to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits?

    Formula is quite all right. This code sample calculates sum of cubes of digits for a single number.

    Now you need simply enclose this calculation in a loop which iterates "number" from 10 to 9999 and print only those numbers for which "counter" equals to "number".

Similar Threads

  1. sum of digits
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 02:40 PM
  2. Rookie here, need help with sum of digits program
    By wkellogg10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2013, 07:25 PM
  3. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  4. Replies: 2
    Last Post: February 19th, 2010, 08:10 AM
  5. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 12:33 PM