Results 1 to 9 of 9
- 03-17-2011, 01:58 AM #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
{
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; 03-17-2011 at 02:03 AM.
- 03-17-2011, 02:07 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Try calc += ((2*i)+1).
Also, calculate by hand what got code is actually doing.
- 03-17-2011, 02:41 AM #3
Member
- 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; 03-17-2011 at 03:50 AM.
- 03-17-2011, 02:45 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Set calc to 0 then try putting
Java Code:calc+=((2*I) + 1);
- 03-17-2011, 03:04 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
- 03-17-2011, 03:43 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
+= is just programmer short hand. In the example it means
There is also one for each operatorJava Code:calc = calc + ((2 * i) + 1);
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.Java Code:+= *= -= /= %=
You would prompt the user for some number and then do
So if the user enters 3 it would set calc to 7.Java Code:2 * n _+ 1
In the loop you were doing
Where you thought it was doing the full operation each time it was simply incrementing an already determined variable(calc).Java Code:calc = calc + 1; or calc += 1; or calc++;
if the user entered 2 it would be
and then it would increment it 3 times(0, 1, 2), producing 8.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
Also, if your done please mark your thread solved with the thread tools.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.
- 03-17-2011, 03:49 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 27
- Rep Power
- 0
Thank you for being so helpful :)
- 03-17-2011, 11:09 PM #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);
}
}
}
- 03-17-2011, 11:12 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Basic Calculator
By trtoy in forum New To JavaReplies: 1Last Post: 12-25-2009, 01:06 AM -
Basic Calculator
By Rose88 in forum Java AppletsReplies: 3Last Post: 06-25-2009, 12:34 AM -
Java Calculator
By aapanju in forum New To JavaReplies: 3Last Post: 04-17-2008, 05:33 AM -
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04-16-2008, 01:19 PM -
Create a Calculator in Java
By Albert in forum New To JavaReplies: 2Last Post: 07-04-2007, 08:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks