I began with a code to print sum of first n no.
Explaination:
- First I imported scanner class for input.
- Created a class named loop and then asked for user to enter a number till which he needs sum.
- Then I declared a variable s, whose value I initated as 0, so that I can perform add operation later on it.
- Then I used for loop, in which I incremented value of i till it reaches the desired number of times as given by user.
- 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)