1. Member
Join Date
Nov 2008
Posts
4
Rep Power
0

I was reading a java workbook and discover this question:
-Write a program to determine the sum of the integers falling between 300 and 1000, inclusively.

Problem: I know this is very basic but it seems that I can't do it.... can anyone show me an example and teach me pls? XD

ps: "I'm only good in if and else statement".... =.="

2. Originally Posted by Queenzy
I was reading a java workbook and discover this question:
-Write a program to determine the sum of the integers falling between 300 and 1000, inclusively.

Problem: I know this is very basic but it seems that I can't do it.... can anyone show me an example and teach me pls? XD

ps: "I'm only good in if and else statement".... =.="
Yes this is very simple. What you have tried so far?

3. As you probably already know, you're much better off by not giving up from the very start. Try it first on your own, then if it fails show us your code. Else, how will we know what it is exactly that you don't understand?

4. Member
Join Date
Nov 2008
Posts
4
Rep Power
0
public class Lala{
public static void main (String[] args){
int sum=0;
for (int x=300;x<=1000;x++){
sum+=x;
}
System.out.println("Sum="+sum);
}
}

-I just can't display properly XD
-The result was Sum=455650
-how to make it fall instead of suming all up?

5. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
9
Originally Posted by Queenzy
public class Lala{
public static void main (String[] args){
int sum=0;
for (int x=300;x<=1000;x++){
sum+=x;
}
System.out.println("Sum="+sum);
}
}

-I just can't display properly XD
-The result was Sum=455650
-how to make it fall instead of suming all up?
What are you expecting as output, as opposed to what you got? Sorry, but I don't understand your last question.

6. Senior Member
Join Date
May 2008
Location
Makati, Philippines
Posts
234
Rep Power
9
Java Code:
public class Lala{
public static void main (String[] args){
int sum=0;
for (int x=300;x<=1000;x++){
sum+=x;
}
System.out.println("Sum="+sum);
}
}
***
sum += x;
is also the same as
sum = sum + x;
Thats why it will print the sum from 300 to 1000.

Now what is your question here?

7. Originally Posted by Queenzy
-I just can't display properly XD
What you mean XD?

Originally Posted by Queenzy
-The result was Sum=455650
Is that a wrong value?

Originally Posted by Queenzy
-how to make it fall instead of suming all up?
What you mean by fall? I think you want to add numbers from 301 to 999, is it?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•