Java Program To Calculate The Sum of Natural Numbers
Java Program To Calculate The Sum of Natural Numbers
Output
Sum = 5050
The above program loops from 1 to the given num (100) and adds all numbers to the
variable sum .
Output
Sum = 1275
In the above program, unlike a for loop, we have to increment the value of i inside the
body of the loop.
Though both programs are technically correct, it is better to use for loop in this case. It's
because the number of iteration (up to num ) is known.
Visit this page to learn how to find the sum of natural numbers using recursion.