In this program, you'll learn to calculate the sum of natural numbers using for loop and while loop in Java.
To understand this example, you should have the knowledge of the following Java programming topics:
- Java for Loop
- Java while and do...while Loop
The positive numbers 1, 2, 3... are known as natural numbers and its sum is the result of all numbers starting from 1 to the given number.
For n, the sum of natural numbers is:
1 + 2 + 3 + ... + nExample 1: Sum of Natural Numbers using for loop
public class SumNatural { public static void main(String[] args) { int num = 100, sum = 0; for(int i = 1; i <= num; ++i) { // sum = sum + i; sum += i; } System.out.println("Sum = " + sum); } }Output
Sum = 5050The above program loops from 1 to the given num(100) and adds all numbers to the variable sum.
You can solve this problem using a while loop as follows:
Example 2: Sum of Natural Numbers using while loop
public class SumNatural { public static void main(String[] args) { int num = 50, i = 1, sum = 0; while(i <= num) { sum += i; i++; } System.out.println("Sum = " + sum); } }Output
Sum = 1275In the above program, unlike a for loop, we have to increment the value of i inside the body of the loop.
Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iteration (up to num) is known.
Visit this page to learn how to find the sum of natural numbers using recursion.
Improve Article
Save Article
Improve Article
Save Article
Print first N natural numbers using an iterative approach i.e. using for loop. For loop has three parameters initialization, testing condition, and increment/decrement.
Input: N = 10 Output: First 10 Numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Sum of first 10 Natural Number = 55 Input: N = 5 Output: First 5 Numbers = 1, 2, 3, 4, 5 Sum of first 5 Natural Number = 15Approach
- Start for loop initialization with i = 1.
- Write testing condition as i <= N.
- Add increment statement as i++ or i+=1.
- Initialize a variable sum with 0.
- Start adding i with the sum at each iteration of for loop and print i.
- Print sum at the end for loop.
Below is the implementation of above approach
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 10;
int sum = 0;
System.out.print("First " + N + " Numbers = ");
for (int i = 1; i <= N; i++) {
System.out.print(i + " ");
sum += i;
}
System.out.println();
System.out.println("Sum of first " + N
+ " Natural Number = " + sum);
}
}
Output
First 10 Numbers = 1 2 3 4 5 6 7 8 9 10 Sum of first 10 Natural Number = 55Time Complexity: O(n)
Alternate Approach
- Start for loop initialization with i = 1.
- Write testing condition as i <= N.
- Add increment statement as i++ or i+=1.
- Start Printing i for each iteration.
- Print sum using first N natural number formula at the end of for loop.
Below is the implementation of the above approach
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 5;
System.out.print("First " + N + " Numbers = ");
for (int i = 1; i <= N; i++) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("Sum of first " + N
+ " Natural Number = " + (N*(N+1))/2);
}
}
Output
First 5 Numbers = 1 2 3 4 5 Sum of first 5 Natural Number = 15Time Complexity: O(n)
Auxiliary Space: O(1) as it is using constant space for variables