Find the Sum of Natural Numbers Java

PROGRAM TO FIND THE SUM OF N NATURAL NUMBERS


public class SumNatural {

    public static void main(String[] args) {

        int num = 50, i = 1, sum = 0;

        while(i <= num)
        {
            sum += i;
            i++;
        }
        System.out.println("Sum = " + (num*(num+1)) / 2);
System.out.println("Sum = " + sum); } }


OUTPUT
Sum = 1275
Sum = 1275

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java