• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
June 08, 2022 |1.0K Views
Java Program to Print the Sum of First 'n' Natural Numbers
Description
Discussion


In this video, we will see a JAVA program for sum of first n natural numbers.

Examples:
1. Input : n = 4
Output : 10
Explanation :
Note that: 1 + 2 + 3+4 = 10

2.Input : 6
Output : 21
Explanation :
Note that: 1 + 2 + 3 + 4 + 5 +6 = 21

In this video we see two different approaches for Sum of first n natural numbers in Java:
1. Using Loop
Method 1: Using For loop
Method 2: Using While loop
2. Using Mathematical formula: [ Sum of first n natural numbers = (n*(n+1))/2]

Program to find the sum of first n natural numbers:
https://www.geeksforgeeks.org/program-find-sum-first-n-natural-numbers/