The sum of the first n natural numbers



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:


$ S_n=1+2+...+n=? $


The answer is


$$S_n=\frac{n(n+1)}{2}$$
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 $$S_n=\frac{n(n+1)}{2}$$
//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);

}
}

The sum of the first n natural numbers The sum of the first n natural numbers Reviewed by Animesh Chatterjee on January 03, 2012 Rating: 5

1 comment:

  1. 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

Powered by Blogger.