sum of cubes

The program asks for the sum squares and the sum of cubes to imput tha

Page 1 of 1

6 Replies - 9300 Views - Last Post: 05 March 2009 - 10:50 AM Rate Topic: -----

#1 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 182
  • Joined: 05-March 09

sum of cubes

Post icon  Posted 05 March 2009 - 10:26 AM

import java.io.*;
import java.util.*;
public class lab4_2 {
public static void main(String[]args){

Scanner in=new Scanner(System.in);
System.out.println("Input n:");
double n;
n=in.nextDouble();

final double side1;
side1 = Math.pow(1,3)+Math.pow(2,3)+Math.pow(n,3);
final double side2;
side2 = Math.pow((1+2+n),2.0);

while(n>0){
if(side1 == side2)
System.out.print("The two sides are equal");

}//end while
System.out.printf("side1= %5.2f\n", side1);
System.out.printf("Side2= %5.2f\n", side2);
}//end main
}// end class

Is This A Good Question/Topic? 0
  • +

Replies To: sum of cubes

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3196
  • View blog
  • Posts: 19,226
  • Joined: 14-September 07

Re: sum of cubes

Posted 05 March 2009 - 10:32 AM

Java does not have a System.out.printf(), it does have system.out.println()/print().

In addition, put your question in the post itself rather then the title, it gets cut off.

There's some problems with your loop:

while(n>0){
if(side1 == side2)
System.out.print("The two sides are equal");

}//end while



n isn't inputted here, so this will execute infinitely as long as the initial input is greater then zero.
Was This Post Helpful? 0
  • +
  • -

#3 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 182
  • Joined: 05-March 09

Re: sum of cubes

Posted 05 March 2009 - 10:36 AM

thank you...
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: sum of cubes

Posted 05 March 2009 - 10:37 AM

View PostKYA, on 5 Mar, 2009 - 11:32 AM, said:

Java does not have a System.out.printf()


...Sorry, you're wrong. ;)

Click Here
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3196
  • View blog
  • Posts: 19,226
  • Joined: 14-September 07

Re: sum of cubes

Posted 05 March 2009 - 10:39 AM

Holy shit it does. I checked the API before to be sure too, I must have missed it. I am a horrible person.

There's no need to use it in this instance though ;)

This post has been edited by KYA: 05 March 2009 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: sum of cubes

Posted 05 March 2009 - 10:41 AM

View PostKYA, on 5 Mar, 2009 - 11:39 AM, said:

Holy shit it does. I checked the API before to be sure too, I must have missed it. I am a horrible person.

There's no need to use it in this instance though ;)


You're half right...the System class does not have a printf() method...but we want too look at the out (PrintStream) object of the System class...which does have the method.
Was This Post Helpful? 0
  • +
  • -

#7 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: sum of cubes

Posted 05 March 2009 - 10:50 AM

Yep there is a printf function,

Also to the topic creator make sure to post using code tags :code: You can edit your post to do this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1