# Java program to calculate the sum of natural numbers

https://wgi.wwiftidea.com

Sure, here's a Java program to calculate the sum of natural numbers:

public class SumOfNaturalNumbers { public static void main(String[] args) { int n = 10; int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } System.out.println("Sum of natural numbers from 1 to " + n + " = " + sum); } }

In this program, we first define an integer `n`

to represent the upper limit of the natural numbers to sum. We then initialize an integer `sum`

to 0.

We use a `for`

loop to iterate from 1 to `n`

and add each number to `sum`

. Finally, we print out the sum of the natural numbers from 1 to `n`

.

Note that the formula for the sum of the natural numbers from 1 to `n`

is:

sum = (n * (n + 1)) / 2

This formula can be used to calculate the sum of natural numbers without using a loop, which is more efficient for large values of `n`

.