The sum of the first n natural numbers are written as

1=1

1+2=3

1+2+3=6

1+2+3+4=10

1+2+3+4+5=15

What is the formula to calculate this sum:

The answer is

Logic Source-http://www.9math.com/book/sum-first-n-natural-numbers

## simple logic

1. Take the number from user.2. Add them up one by one

## Implementation

1. Take the number from user.2. Add them up one by one

//after taking input from user convert that to integer

private static void calculate_sum(int i) {

int sum=0;

int num=i;

for (int j=1;j<=num;j++)

{

sum+=j;

}

System.out.print(sum);

}

But as per mathematics there is one formula to calculate that

## Formula logic

1. Take the number from user.2. Add them up one by one

## Implementation

1. Take the number from user.2. put the formula

//after taking input from user convert that to integer

private static void calculate_sum(int i) {

//Initialize sum as 1 and a temp variable called num

int sum=1;

int num=i;

// implement formula

sum=i*(i+1);

sum=sum/2;

System.out.print(sum);

}

}

Nicely described natural numbers and their addition,I like this blog because I get the formula which is use to find sum of natural numbers in series,thanks.

ReplyDelete