DEV Community

Chhavi Joshi
Chhavi Joshi

Posted on

Learning java

I began with a code to print sum of first n no.

Image description

Explaination:

  1. First I imported scanner class for input.
  2. Created a class named loop and then asked for user to enter a number till which he needs sum.
  3. Then I declared a variable s, whose value I initated as 0, so that I can perform add operation later on it.
  4. Then I used for loop, in which I incremented value of i till it reaches the desired number of times as given by user.
  5. Then this value of i (incrementing) would be added to s which was initially 0.

In this manner we'll get sum of first n numbers

Top comments (0)