Java Program to Find Sum of N Natural Numbers
You can find the sum of n natural numbers in Java using the formula
sum = n * (n + 1) / 2 or by looping from 1 to n and adding each number.Examples
Input5
OutputSum of first 5 natural numbers is 15
Input10
OutputSum of first 10 natural numbers is 55
Input1
OutputSum of first 1 natural number is 1
How to Think About It
To find the sum of the first n natural numbers, think of adding all numbers from 1 up to n. You can do this by either adding each number one by one or by using the formula
n * (n + 1) / 2 which gives the total directly.Algorithm
1
Get the input number n from the user.2
Calculate the sum using the formula sum = n * (n + 1) / 2.3
Print the sum.Code
java
import java.util.Scanner; public class SumNaturalNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = scanner.nextInt(); int sum = n * (n + 1) / 2; System.out.println("Sum of first " + n + " natural numbers is " + sum); scanner.close(); } }
Output
Enter a positive integer: 5
Sum of first 5 natural numbers is 15
Dry Run
Let's trace the input 5 through the code
1
Input
User enters n = 5
2
Calculate sum
sum = 5 * (5 + 1) / 2 = 5 * 6 / 2 = 30 / 2 = 15
3
Output
Print "Sum of first 5 natural numbers is 15"
| n | sum |
|---|---|
| 5 | 15 |
Why This Works
Step 1: Input reading
The program reads the number n from the user using a scanner.
Step 2: Sum calculation
It uses the formula n * (n + 1) / 2 which adds all numbers from 1 to n efficiently.
Step 3: Output display
The program prints the calculated sum with a clear message.
Alternative Approaches
Using loop
java
import java.util.Scanner; public class SumNaturalNumbersLoop { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = scanner.nextInt(); int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } System.out.println("Sum of first " + n + " natural numbers is " + sum); scanner.close(); } }
This method uses a loop to add each number one by one, which is easy to understand but less efficient for very large n.
Using recursion
java
import java.util.Scanner; public class SumNaturalNumbersRecursion { public static int sumNatural(int n) { if (n == 1) return 1; return n + sumNatural(n - 1); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = scanner.nextInt(); int sum = sumNatural(n); System.out.println("Sum of first " + n + " natural numbers is " + sum); scanner.close(); } }
This method uses recursion to add numbers, which is elegant but can cause stack overflow for very large n.
Complexity: O(1) time, O(1) space
Time Complexity
Using the formula, the calculation is done in constant time without loops.
Space Complexity
Only a few variables are used, so space is constant.
Which Approach is Fastest?
The formula method is fastest; looping or recursion take O(n) time and more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Formula | O(1) | O(1) | Fastest and simplest for any n |
| Loop | O(n) | O(1) | Easy to understand, good for small n |
| Recursion | O(n) | O(n) | Elegant but risky for large n due to stack |
Use the formula
n * (n + 1) / 2 for the fastest and simplest solution.Forgetting to use integer division properly or using zero or negative numbers as input.