programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# sum of numbers between two integers

Hans Baba
Ranch Hand
Posts: 36
Hello guys its me again

I was trying to get sum of numbers between two given integer and managed to do it.

public int sum(int a, int b) {
int sum=0;
while(a <= b) {
sum+=a;
a++;
}
return sum;

}

in here; when I enter the values i.e 2 and 5, it calculates 2+3+4+5 and gives me the result 14.

my question is, how could I exclude a and b in this calculation ( in this case exclude 2 and 5 and return 3+4 )

Bonus question ; how could I only calculate the odd (or even) numbers between those integers?

Thanks in avance.

Dave Tolls
Ranch Foreman
Posts: 3056
37
How would you do these things by hand, ie on a piece of paper?

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Forget about programming. Forget about computers. Take out some paper, pencils, and erasers. I'm going to ask you a question.

"Give me the sum of all integers between, but not including, 17 and 84."

How would YOU do it?

Then, look at your loop. What numbers are you adding up? Is there a way you could stop a little sooner (so as to not include b)? alternately, if you know your sum at the end includes a and b, could you not subtract them out? Is there a way you could perhaps start not with a, but with a + 1?

To just get the odds, you could again do it many ways. You could only add a number to the sum if it is odd. You could figure out if your starting point is odd or even, adjust as needed, then increment by 2 instead of 1.

Or, you could research the formula for computing the sum of a range of integers and just calculate it directly.

Hans Baba
Ranch Hand
Posts: 36
ok, I know this is as easy as 1 2 3 for you, but Im very new in this and it took me so much time to write these few lines. (yeah I know sounds funny.). Of course I know how to do it on a paper,but applying that in java is a bit complicated for me.

Campbell Ritchie
Marshal
Posts: 56533
172
No, it is writing down what you have on paper that is the problem. If you write it down simply enough it will become easy to convert to Java. Write it down so simply that it is all in words of one syllable.

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
• 2
Hans Baba wrote: Of course I know how to do it on a paper,but applying that in java is a bit complicated for me.

We are not making light of it. Seriously - this is how you learn to program.

Programming isn't writing lines of code. Programming is THINKING. you should expect to spend 70-90% of your time thinking, and only 10-30% writing lines of java code in your editor.

So, literally write down the steps of how you would do it. Then, pretend you are going to hand those directions to a child. Would they be able to follow them? Are they clear on what to do in ALL cases? If not, revise them, making them simpler. You may have to break down each into multiple, smaller steps.

When you have it written out, post it here.

Winston Gutkowski
Bartender
Posts: 10575
66
Hans Baba wrote:Of course I know how to do it on a paper,but applying that in java is a bit complicated for me.

You may think so right now, but oddly enough it isn't. And once you've done the "paperwork", we'll be more than happy to help you with any "translation" problems you have.

The problem is that when most people say "of course I know how to do it on paper", they mean they think they do, but they haven't actually tried it.

Having a Java compiler in front of you doesn't suddenly make you smarter; and believe me, if you can't do as Fred suggests, and describe the problem in English (or your native language), you'll never write a decent program to do it in Java.

HIH

Winston

Greenhorn
Posts: 26
• 1

Please make A change like this......

public int sum(int a, int b) {
int sum=0;
while(a < b-1) {
++a;
sum+=a;

}

THIS IS FOR ODDSUM AND EVENSUM......

public void sum(int a, int b) {
int sum=0;
int oddSum=0;
int evenSum=0;
while(a < b-1) {
++a;
sum+=a;
if(a%2==0){
evenSum+=a;
}else{
oddSum+=a;
}
}
System.out.print("sum : "+sum+" even : "+evenSum+" odd: "+oddSum);
}

Hans Baba
Ranch Hand
Posts: 36

Dave Tolls
Ranch Foreman
Posts: 3056
37
• 1
Not too sure of the culture on this forum, and I'm sure one of the regulars will inform me, but is spoon feeding frowned upon?

Winston Gutkowski
Bartender
Posts: 10575
66
Dave Tolls wrote:Not too sure of the culture on this forum, and I'm sure one of the regulars will inform me, but is spoon feeding frowned upon?

Yes it is, but unfortunately the damage has been done now.

@khadar: Please DontBeACodeMill (←click); especially in the 'Beginning Java' forum. I'm sure your motives were great; but we prefer beginners to find answers for themselves, rather than have them handed to them. It may take a bit longer, but it's actually very rewarding to "see the light-bulb come on".

Winston

Greenhorn
Posts: 6
step1: increment the value of first num(lower limit) and
write a for loop that starts from incremented first value and <last number, write the code that finds the sum,

if even number ;
check the first number if its even or odd,
if odd, then add 1 and iterate the loop for every2 elements;
write code that finds the sum;
similalry for odd numbers too;

 It is sorta covered in the JavaRanch Style Guide.