Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,475 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,736 people online right now. Registration is fast and FREE... Join Now!




While loop

 

While loop, total up the even number from 2 to 200

karengsh

28 Jun, 2009 - 10:18 PM
Post #1

New D.I.C Head
*

Joined: 18 May, 2009
Posts: 36



Thanked: 1 times
My Contributions
Hi,

I just can't figure out how to use the while loop to do the totalling up of even numbers in 2 to 300.

Here's the program that I tried :-

Failed miserably. Pls help me. Tks.
CODE

class NumberSequence {
public static void main (String[] args){
   int num = 2;
   while (num <= 200){
      int total = num;
      num+=2;
   System.out.print(total);}}}


Mod edit - Please code.gif

User is offlineProfile CardPM
+Quote Post


BetaWar

RE: While Loop

28 Jun, 2009 - 10:27 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
Please remember to put your code in the code tags, like so:
code.gif

Now, the code should look like so:
CODE
class NumberSequence {
  public static void main (String[] args){
    int num = 2;
    int total = 0;
    while (num <= 300){
      total += num % 2 == 0?num:0;
      num++;
      System.out.print(total);
    }
  }
}


HTMS
User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: While Loop

28 Jun, 2009 - 10:30 PM
Post #3

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
Hope this helps! Goodluck!

Nevermind don't feel like doing your homework. Just saw your other 4 Threads.


This post has been edited by Fuzzyness: 28 Jun, 2009 - 10:50 PM
User is offlineProfile CardPM
+Quote Post

Gloin

RE: While Loop

29 Jun, 2009 - 10:08 AM
Post #4

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 3,528



Thanked: 143 times
Dream Kudos: 75
My Contributions
Use the formula for a arithmetic sum instead.

This post has been edited by Gloin: 29 Jun, 2009 - 11:17 PM
User is offlineProfile CardPM
+Quote Post

computerfox

RE: While Loop

29 Jun, 2009 - 10:13 AM
Post #5

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
you can always use an extra variable... hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

karengsh

RE: While Loop

29 Jun, 2009 - 05:44 PM
Post #6

New D.I.C Head
*

Joined: 18 May, 2009
Posts: 36



Thanked: 1 times
My Contributions
This is for the benefit of those who wish to know how to do:-

int x = 2;

while ( x <= 200)
if ( x%2 == 0) // this check if the numbers between 2 & 200 is an even number, if yes, it goes next
total += x; // this would add up the even numbers
x++; // this will increaes x
}
System.out.print(total)
}}}

I thanked God who sent an Guardian Angel to me who helped me with this question and it happened to be my test question !!! Thank God!
User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: While Loop

29 Jun, 2009 - 08:01 PM
Post #7

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
QUOTE(karengsh @ 29 Jun, 2009 - 05:44 PM) *

This is for the benefit of those who wish to know how to do:-

int x = 2;

while ( x <= 200)
if ( x%2 == 0) // this check if the numbers between 2 & 200 is an even number, if yes, it goes next
total += x; // this would add up the even numbers
x++; // this will increaes x
}
System.out.print(total)
}}}

I thanked God who sent an Guardian Angel to me who helped me with this question and it happened to be my test question !!! Thank God!


As Beta has already told you as previous mods have as well...

code.gif

and I dont suppose D.I.C. has a Religion Policy? huh2.gif

This post has been edited by Fuzzyness: 29 Jun, 2009 - 08:02 PM
User is offlineProfile CardPM
+Quote Post

jbashask

RE: While Loop

2 Jul, 2009 - 02:23 AM
Post #8

New D.I.C Head
*

Joined: 2 Jul, 2009
Posts: 1

QUOTE(karengsh @ 28 Jun, 2009 - 10:18 PM) *

Hi,

I just can't figure out how to use the while loop to do the totalling up of even numbers in 2 to 300.

Here's the program that I tried :-

Failed miserably. Pls help me. Tks.
CODE

class NumberSequence {
public static void main (String[] args){
   int num = 2,total;
   while (num <= 200){
          total += num;
      num+=2;}
   System.out.print(total);}}


Mod edit - Please code.gif


This post has been edited by jbashask: 2 Jul, 2009 - 02:25 AM
User is offlineProfile CardPM
+Quote Post

DecafJava

RE: While Loop

2 Jul, 2009 - 10:53 AM
Post #9

New D.I.C Head
*

Joined: 18 May, 2009
Posts: 47


My Contributions
Here is the simple way:

CODE

public static void main(String[] args) {
     int sum = 0;
     for (int i = 2; i <= 300; i += 2) {
          sum += i;
     }
     System.out.println("The sum is: " + sum);
}


You don't have to keep checking if it is an even number or not. When you start with 2 (the first even number) and keep on incrementing by 2, you will get all the even numbers.

This post has been edited by DecafJava: 2 Jul, 2009 - 10:57 AM
User is offlineProfile CardPM
+Quote Post

karengsh

RE: While Loop

5 Jul, 2009 - 07:31 PM
Post #10

New D.I.C Head
*

Joined: 18 May, 2009
Posts: 36



Thanked: 1 times
My Contributions
QUOTE(DecafJava @ 2 Jul, 2009 - 10:53 AM) *

Here is the simple way:

CODE

public static void main(String[] args) {
     int sum = 0;
     for (int i = 2; i <= 300; i += 2) {
          sum += i;
     }
     System.out.println("The sum is: " + sum);
}


You don't have to keep checking if it is an even number or not. When you start with 2 (the first even number) and keep on incrementing by 2, you will get all the even numbers.

Thanks but I was looking hard for the while loop way be'cos last minute discovered I haven't mastered that for my exam....
User is offlineProfile CardPM
+Quote Post

mostyfriedman

RE: While Loop

6 Jul, 2009 - 02:43 AM
Post #11

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 3,148



Thanked: 355 times
Dream Kudos: 600
Expert In: Learning

My Contributions
as gloin said, you can use the formula for arithmetic sum..will be done in O(1)
User is offlineProfile CardPM
+Quote Post

karengsh

RE: While Loop

6 Jul, 2009 - 03:18 AM
Post #12

New D.I.C Head
*

Joined: 18 May, 2009
Posts: 36



Thanked: 1 times
My Contributions
QUOTE(mostyfriedman @ 6 Jul, 2009 - 02:43 AM) *

as gloin said, you can use the formula for arithmetic sum..will be done in O(1)



Thanks for showing. I know that the For is a simpler way. Anyway, thanks so much for being so willing to teach and share. Really appreciate it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:23AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month