954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

counting loop

import java.util.Scanner;
class wloop
{ 
	public static void main(String[] args)
	{ 
int counter=2;
int sum=0;
while( counter<=200 )
{
sum = sum+counter ;
counter = counter+2;
}
System.out.println("The sum of integers from 2 to 200 is: " +sum);
 		}
 		}
import java.util.Scanner;
class wloop
{ 
    public static void main(String[] args)
    { 
        int sum=2;
        while (sum <=200)
        { 
        { 
        System.out.println(sum+ "");
        sum+=2;
         }
         }
         }
         }

the first code count out the even number 2 - 200 and the secound show the line 2 4 ... 200. but i dont know how to to it like its going to to it in 1 program like this.
2
4
.
200
The sum of integers from 2 to 200 is:
can anyone help me on how i can do it white out a if?

Kalle21
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Didn't quite get your question... What EXACTLY are you looking for ?

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

i want the program to count out even number between 0 - 200 and sum up the numbers.
like this.
2
4
6
and so on
200
the sum of it is : 10100

Kalle21
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Just print counter inside while loop of your first code. You will get
2
4
6 etc
Rest of the code seems fine

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

can you show me what you meen im new to this

Kalle21
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 
System.out.println(sum+ "");
        sum+=2;

in your other class, you're doing actually the same as this, but there you are using counter.
so, copy-paste the print command in the other class, and change sum to counter.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: