Monday, May 24, 2010

How do I write a Java "for" loop that sums up the difference of two user inputted integers?

Specifically, I need the user to enter two integers, a and b. That part I got, i think. But then I need it to compute the sum of all integers from a to b, inclusive. And then I need it to ouput that value.

How do I write a Java "for" loop that sums up the difference of two user inputted integers?
int sum = 0;


for(int i = a; i %26lt;= b; i++)


{


sum += i;


}





System.out.println("The sum a to b inclusive: " + sum);





Let me know if this doesn't make sense!
Reply:you can use the following code:





public class Sum


{





public Sum()


{


int a;


int b;


}





public int add(int a, int b)


{


return a + b;


}


}

garden design

No comments:

Post a Comment