# help with basic sum calculator in java

• 03-17-2011, 02:58 AM
java157
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! :)
• 03-17-2011, 03:07 AM
sunde887
Try calc += ((2*i)+1).

Also, calculate by hand what got code is actually doing.
• 03-17-2011, 03:41 AM
java157
Quote:

Originally Posted by sunde887
Try calc += ((2*i)+1).

Didn't work

Quote:

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:
• 03-17-2011, 03:45 AM
sunde887
Set calc to 0 then try putting
Code:

`calc+=((2*I) + 1);`
• 03-17-2011, 04:04 AM
java157
Quote:

Originally Posted by sunde887
Set calc to 0 then try putting
Code:

`calc+=((2*I) + 1);`

It worked :D
• 03-17-2011, 04:43 AM
sunde887
+= is just programmer short hand. In the example it means
Code:

`calc = calc + ((2 * i) + 1);`
There is also one for each operator
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
Code:

`2 * n _+ 1`
So if the user enters 3 it would set calc to 7.

In the loop you were doing
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
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
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.
• 03-17-2011, 04:49 AM
java157
Thank you for being so helpful :)
• 03-18-2011, 12:09 AM
xp_vista007
Look to this :

Quote:

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);
}
}
}
• 03-18-2011, 12:12 AM
sunde887
Why do you want us to look at that code?