my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loops" Watch "Loops" New topic
Author

Loops

Swati Samudra
Greenhorn

Joined: Dec 27, 2000
Posts: 16
I wan't to add integers 1 to 30. How can I do that in java.
I tried using for and while loop but failed to add the integers.
I only get 1,2,3 etc.But I wan't to add 1+2+3 etc.
It has to be
0 + 1 =1
1 + 2 =3
3 + 3 =6
6 + 4 =10
10 + 5 =15
15 + 6 =21 and so on
upto 30.
Please help me or tell me the other way I can do it in Java. Correct me if I am wrong.
YesYouretheOne
Greenhorn

Joined: Jan 17, 2001
Posts: 3
First you can try killing a chicken with a bull's knife by using OOP, ie classes.
main()
{
printf(1+2+3+.....+30);
}
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
YesYourTheOne .... please read here
about our naming policy ... your name does not comply please register again with a valid name.
Also I deleted your prvious post because it was rude and irrelevent to the discussion. Please keep your posts friendly, helpful and above all .. on topic!


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Swati,
This will do what you want,
Swati Samudra
Greenhorn

Joined: Dec 27, 2000
Posts: 16
Thanks Peter Tran,
I tried the coding. It's exactly what I was looking for but was not able to do the way you did it coz I was not using your first println statement:
System.out.println(sum+"+"+i+"=");
Thanks again.
[This message has been edited by Swati Samudra (edited January 18, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Loops