Results 1 to 9 of 9
 03172011, 02:58 AM #1Member
 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
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("Input the upper limit: ");
n = Integer.parseInt(keybd.readLine());
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; 03172011 at 03:03 AM.
 03172011, 03:07 AM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
Try calc += ((2*i)+1).
Also, calculate by hand what got code is actually doing.
 03172011, 03:41 AM #3Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
Didn't work
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
for instance, instead of
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; 03172011 at 04:50 AM.
 03172011, 03:45 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
Set calc to 0 then try putting
Java Code:calc+=((2*I) + 1);
 03172011, 04:04 AM #5Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
 03172011, 04:43 AM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
+= is just programmer short hand. In the example it means
Java Code:calc = calc + ((2 * i) + 1);
Java Code:+= *= = /= %=
You would prompt the user for some number and then do
Java Code:2 * n _+ 1
In the loop you were doing
Java Code:calc = calc + 1; or calc += 1; or calc++;
if the user entered 2 it would be
Java Code:2 * 2 + 1 = 5
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.
 03172011, 04:49 AM #7Member
 Join Date
 Mar 2011
 Posts
 27
 Rep Power
 0
Thank you for being so helpful :)
 03182011, 12:09 AM #8Member
 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);
}
}
}
 03182011, 12:12 AM #9
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
Similar Threads

Basic Calculator
By trtoy in forum New To JavaReplies: 1Last Post: 12252009, 02:06 AM 
Basic Calculator
By Rose88 in forum Java AppletsReplies: 3Last Post: 06252009, 12:34 AM 
Java Calculator
By aapanju in forum New To JavaReplies: 3Last Post: 04172008, 05:33 AM 
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04162008, 01:19 PM 
Create a Calculator in Java
By Albert in forum New To JavaReplies: 2Last Post: 07042007, 08:01 AM
Bookmarks