# Sum Of Squares help

Page 1 of 1

## 8 Replies - 3834 Views - Last Post: 03 February 2010 - 03:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=153737&amp;s=c3684936263d26d1fb685a6901a94bf8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Akilles

Reputation: 0
• 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

• No idea why my code works

Reputation: 435
• Posts: 1,602
• 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;
}
print out sum_variable

```

### #3 Akilles

Reputation: 0
• 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.

### #4 SwiftStriker00

• No idea why my code works

Reputation: 435
• Posts: 1,602
• 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

### #5 Akilles

Reputation: 0
• 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

### #6 erik.price

• D.I.C Lover

Reputation: 485
• 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)

### #7 Akilles

Reputation: 0
• 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???

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11135
• Posts: 41,803
• 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..
}

```

### #9 SwiftStriker00

• No idea why my code works

Reputation: 435
• Posts: 1,602
• Joined: 25-December 08

## Re: Sum Of Squares help

Posted 03 February 2010 - 03:21 PM

Akilles, 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