Java program to calculate the sum of natural numbers

Configurare noua (How To)

Situatie

In this program, you’ll learn to calculate the sum of natural numbers using for loop in Java.

Solutie

Pasi de urmat

public class SumNatural {

public static void main(String[] args) {

int num = 100, sum = 0;

for(int i = 1; i <= num; ++i)
{
// sum = sum + i;
sum += i;
}

System.out.println(“Sum = ” + sum);
}
}

Output:

Sum = 5050

Tip solutie

Permanent

Voteaza

(15 din 25 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?