Lol I shouldn't be posting this here...but anyway, I'm trying to find a summation 1/(a+b)
where it repeats 100 times and a and b both increase by 1 every time. This is what I have so far, and it looks right, but I keep on getting 0 D:
public class Summation {
public static void main(String[] args) {
int a = 1;
int b = 2;
double result = 1/(a+b);
int beginloop;
int endloop=100;
for (beginloop = 1;beginloop<endloop;beginloop++){
a = a+1;
b = b+1;
result = result + 1/(a+b);
}
System.out.println(result);
}
}Last edited by matthew8092001 (2011-06-24 07:12:23)

Offline
Ok, there is a few things I think that would make your code better.
1. The variables in your for loop should all be local to that loop
example pulled from google:
class ForDemo {
public static void main(String[] args){
//loop 10 times
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
2. Use proper addition operators
don't use result= result + 1
Use instead result+=1
}
If you want to add 1 to a number just use ++
ex:
a++;
is the same as a=a+1;
3. This result = result + 1/(a+b);
is incorrect
It should be result = (result + 1)/(a+b);
or simply result += 1/(a+b);
If that still doesn't work change all your number variables to doubles.
Offline