Results 1 to 3 of 3
Thread: Do While Loop
- 02-14-2010, 02:15 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 2
- Rep Power
- 0
Do While Loop
Hi,
I am new to Java and would appreciate it if someone could me with the following:
Write a do while loop so it calculates: 1; 1+2; 1+2+3; 1+2+3+4; 1+2+3+4+5.
At the moment, I have done a basic do while loop for displaying 1 to 5.
public static void main(String[] args){
int i =1;
do
{
System.out.println(+ i);
i++;
}while(i <=5);
}
}
Thanks!
-
Declare an int variable (I'd call it "sum") before the do-while loop and add i to the sum value inside of the loop.
- 02-14-2010, 03:29 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
So yeah, code tags, read about them :D
As per your question, I'm a bit confused, do you want to calculate the sum of 1, 1+2, 1+2+3, or do you want to print out the expression without calculating? If all you want to do is the sum, you were pretty well on your way, not much else needs to be done to your code.
Similar Threads
-
for loop help
By soc86 in forum New To JavaReplies: 9Last Post: 01-24-2011, 09:45 PM -
using for loop...pls help
By princess.blue in forum EclipseReplies: 2Last Post: 12-13-2009, 09:30 AM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
Need help with While Loop
By mrdestroy in forum New To JavaReplies: 14Last Post: 10-20-2008, 02:29 PM -
while loop
By michcio in forum New To JavaReplies: 5Last Post: 01-27-2008, 12:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks