Sum Of Squares help

It doesn't work,

Page 1 of 1

8 Replies - 3508 Views - Last Post: 03 February 2010 - 03:21 PM Rate Topic: -----

#1 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 07-January 10

Sum Of Squares help

Posted 03 February 2010 - 01:10 PM

In the programm below it must do this for example: if the user enters 5, the program should compute and display 1+4+9+16+25=55
How can i split and Height squared them...I am new in java so please understand me....


import javax.swing.JOptionPane;


public class Square {
public static void main(String args[]){
    String numb;
    int numb1,i=0,xnum = 0;
    numb= JOptionPane.showInputDialog("Input a  integer number");
    numb1=Integer.parseInt(numb);
    if (numb1<=0){
       JOptionPane.showMessageDialog (null,"Enter a Positive Number Only!"
               ,"Attention",JOptionPane.WARNING_MESSAGE );

    }
    
  [color="#FF0000"]while (i<numb1) 
 xnum= numb1*numb1;
 i++;[/color]
    JOptionPane.showMessageDialog(null,"The sum of the square is " +xnum
            ,"Result",JOptionPane.INFORMATION_MESSAGE);
}
}




i am stuck here.....

Is This A Good Question/Topic? 0
  • +

Replies To: Sum Of Squares help

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Sum Of Squares help

Posted 03 February 2010 - 01:20 PM

heres some psuedo code that will get u going:
sum_variable  = 0;
for( each number, called i starting at one until the input_number ){
   cur_iteration_value = using Math's power method raise the current number i by 2;
   add cur_iteration_value to sum_variable 
}
print out sum_variable


Was This Post Helpful? 0
  • +
  • -

#3 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 07-January 10

Re: Sum Of Squares help

Posted 03 February 2010 - 01:30 PM

I don't know what cur_iteretion value is...
can you be a little more spesific pls?
when i run the programm i input for example 5 but then it must do this as a result : 1+4+9+16+25=55

it must seperates the number 5 times and then height squered them and sum up them.
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Sum Of Squares help

Posted 03 February 2010 - 01:34 PM

i just gave that an arbitrary name to store the math i just did. Since its going through a loop, i called it the current iteration's math result, essentially

This post has been edited by SwiftStriker00: 03 February 2010 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 07-January 10

Re: Sum Of Squares help

Posted 03 February 2010 - 01:52 PM

xnum=numb1^2; i wrote this command and it's not working as i want.....
i want to height square it but it doesn't work and also i want to separate themm too and height square them for examble if i put 5 to separate the 5 to 5 digit like that:1^2 + 2^2 +3^2 + 4^2 +5^2=55
Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Sum Of Squares help

Posted 03 February 2010 - 01:56 PM

^ means exclusive or (XOR), not power

To raise something to a power, do this Math.pow(base, exponent)
Was This Post Helpful? 1
  • +
  • -

#7 Akilles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 07-January 10

Re: Sum Of Squares help

Posted 03 February 2010 - 02:13 PM

yes thank you i height squered them....but now i want to split the nuumber like when i put 5 i want to do 1^2+3^2+4^2+5^2=55 i want to do that how can i do it???
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10658
  • View blog
  • Posts: 39,571
  • Joined: 27-December 08

Re: Sum Of Squares help

Posted 03 February 2010 - 02:17 PM

Use a loop. I think SwiftStriker provided some pseduo-code for this. Also, here is how a for loop works:
for(variable-initialization; boolean-condition; variable-modification){
  ..code..
}


Was This Post Helpful? 0
  • +
  • -

#9 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Sum Of Squares help

Posted 03 February 2010 - 03:21 PM

View PostAkilles, on 03 February 2010 - 03:13 PM, said:

yes thank you i height squered them....but now i want to split the nuumber like when i put 5 i want to do 1^2+3^2+4^2+5^2=55 i want to do that how can i do it???



Ok, your getting confused because you want to do everything in one line, which in programming, this will not happen. we get you want 1^2+2^2+3^2+4^2+5^2=55. So lets see how we do that in steps:

Quote

1^2 = sum1
2^2 = sum2
...
5^2 = sum5
total = sum1 + sum2 + ... + sum5


however we dont want to write out all those steps in our code so we do this:
total = 0;
loop until the variable i starts at 1 and goes to 5, and everytime we loop increase i by 1
sum = i^2
total = total + sum


This post has been edited by SwiftStriker00: 03 February 2010 - 03:21 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1