The sum of the first n natural numbers

1
15
• 1
•
•
•
•
•
•
•
•
•
1
Share 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:  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);

}
}

Don't miss out!

Receive top technical news, lesson ideas, travel tips and more!

Give it a try. You can unsubscribe at any time.

• 1
•
•
•
•
•
•
•
•
•
1
Share

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.