# Thread: help with basic sum calculator in java

1. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## help with basic sum calculator in java

Ok so I am trying to make a sum calculator that calculates the following:

∑ 2n+1

In this problem the user inputs the upper limit of the program and the lower limit is always 0.

This is what I got:

import java.io.*;
public class sum
{
public static void main(String args[]) throws IOException
{

int n;

System.out.print("Input the upper limit: ");

int calc=2*n+1;
for (int i=0; i<=n; i++)
{
calc=calc+i;
}
System.out.println(calc);

}

}

The program works (sort of). When I do test runs it gives me wrong answers, when n >= 2. When I input 0 or 1 for upper limit it gives me correct answer, but when the upper limit is greater than or equal to 2, the answer is wrong.

For instance when I run the program and input 2 for the upper limit, the answer it gives me is 8 ...

but when i do the sum by hand the correct answer is 9:

[2(0)+1] + [2(1)+1] + [2(2)+1]
= 1 + 3 + 5
= 9

This problem has been bugging me for several hours. Any help appreciated thanks! :)
Last edited by java157; 03-17-2011 at 02:03 AM.  Reply With Quote

2. ## Try calc += ((2*i)+1).

Also, calculate by hand what got code is actually doing.  Reply With Quote

3. Member Join Date
Mar 2011
Posts
27
Rep Power
0

##  Originally Posted by sunde887 Try calc += ((2*i)+1).
Didn't work Originally Posted by sunde887 Also, calculate by hand what got code is actually doing.
I tried the code by hand and it works when upper limit is 0 or 1 but not for any number greater than or equal to 2.

When I fix it to work for n=2, it does not work for any other values of n

int calc=2*n+1;
for (int i=0; i<=n; i++)
{
calc +=calc+i;
}
System.out.println(calc);

I tried

int calc=2*n+1;
for (int i=0; i<=n; i++)
{
calc +=calc+i;
}
System.out.println(calc+1); // the bold is where the difference is

AGHHHHH! This problem is giving me a migraine :confused:
Last edited by java157; 03-17-2011 at 03:50 AM.  Reply With Quote

4. ## Set calc to 0 then try putting
Java Code:
`calc+=((2*I) + 1);`  Reply With Quote

5. Member Join Date
Mar 2011
Posts
27
Rep Power
0

##  Originally Posted by sunde887 Set calc to 0 then try putting
Java Code:
`calc+=((2*I) + 1);`
It worked :D
Last edited by java157; 03-17-2011 at 03:49 AM.  Reply With Quote

6. ## += is just programmer short hand. In the example it means
Java Code:
`calc = calc + ((2 * i) + 1);`
There is also one for each operator
Java Code:
```+=
*=
-=
/=
%=```
Now technically what I did borders on the side of spoon feeding, however it seemed like you were putting in the effort. I would, however; like to explain what you were actually doing.

You would prompt the user for some number and then do
Java Code:
`2 * n _+ 1`
So if the user enters 3 it would set calc to 7.

In the loop you were doing
Java Code:
```calc = calc + 1;

or

calc += 1;

or

calc++;```
Where you thought it was doing the full operation each time it was simply incrementing an already determined variable(calc).

if the user entered 2 it would be
Java Code:
`2 * 2 + 1  =  5`
and then it would increment it 3 times(0, 1, 2), producing 8.

You want to set calc to 0, and then each time through the loop you want to perform the operation 2n + 1. This new way evaluates to
Java Code:
```0 + 2 * 0 + 1 == 1

1 + 2 * 1 + 1 == 4

4 + 2 * 2 + 1 == 9```

You can also increase people's rep if they help you by clicking the rep button. I hope I did a good job explaining this all to you.  Reply With Quote

7. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## Thank you for being so helpful :)  Reply With Quote

8. Member Join Date
Mar 2011
Posts
6
Rep Power
0

## Look to this :

import javax.swing.*;

public class mathoperation
{
public static void main(String[] args)
{
double a = new Double(JOptionPane.showInputDialog("First number :")).doubleValue();
double b = new Double(JOptionPane.showInputDialog("Second number : ")).doubleValue();
String s = JOptionPane.showInputDialog("Identificator (just + - * /)");
System.out.println(s);
if (s.equals("+")){
double r = a + b;
JOptionPane.showMessageDialog (null, r);
}
else
if (s.equals("-")){
double r = a - b;
JOptionPane.showMessageDialog (null, r);
}
else
if (s.equals("*")){
double r = a * b;
JOptionPane.showMessageDialog (null, r);
}
else
if (s.equals("/")){
double r = a / b;
JOptionPane.showMessageDialog (null, r);
}
}
}  Reply With Quote

9. ## Why do you want us to look at that code?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•