• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sum of first N numbers  RSS feed

 
Praneet Rohida
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. I am new to java as well as these forums.
i have a very very basic query.
i wrote a program to find the sum of first 10 numbers using the for loop... here is the piece of code


i tried to write the same program using the while loop but with no success.
can someone tell me how can i write this using while loop?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, a for loop is this:

for ( initialize; condition; increment )

(This is a simplification.)

A while loop would look like this:

initialize
while (condition) {
...
increment
}

Can you do the while loop now?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

So far so good, and well done getting that to work. I presume this is an exercise in writing loops, not in knowing the formula × (+ 1) ÷ 2.
To get a while loop to work, I suggest you consider the circumstances under which you would want to repeat. Which values of n would cause you to repeat?
You would also need something to increment n in the loop; n++ would work nicely, but be sure to keep it on a line by itself, because you can get serious confusion with + n++

By the way, write n not N and your output says 20 when you appear to be counting 10. I presume you got the correct answer which is 55?
 
Praneet Rohida
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much..... here is what worked...



i knew the syntax of while loop but dont know why it didnt work when i tried earlier...maybe some unobserved error...the compiler was just getting stuck.... bt nw its working thank you
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you post what you tried before we will be able locate the error.
 
Praneet Rohida
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i didn't save that code and rewrote it in for loop :P so i dont have it
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See what happens if you change <= to = and if you change n++ to ++n or you move the incrementing line to before the addition. Then go through the loop and see if you can work out why.
 
Kat Rollo
Ranch Hand
Posts: 62
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Since your "counter" (N) is initialized to 0, the condition N <= 10 actually iterates (repeats) 11 times, that is, 0 - 10 is 11 times (including 0).
For it to iterate 10 times, you have to say N < 10, which is 0 - 9, that is 10 times (including 0).
If you intend to add integers from 1 through 10, it is more appropriate to say:

The answer should be 55.

In practical application, a for-loop is often used to traverse an array with its index beginning at 0, so it is important to learn how to express conditions properly.
Also as part of accepted naming conventions, variables should follow camelcase format like "taxAmount", "studentName", etc. Capital letters are for "final" variables.
For counters in loops, "ctr" is often used or small letter variables like int, i, int j, int k.

For the while-loop syntax, refer to Knute Snortum's post.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:See what happens if you change <= to =

That's easy. You get a syntax error from the compiler.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn!

I meant to write change to <
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!