Write a program in Java to display the n terms of even natural number and their sum

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 + ... + n

Example 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 = 5050

The 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 = 1275

In 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

  • Read
  • Discuss
  • 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 = 15

    Approach

    1. Start for loop initialization with i = 1.
    2. Write testing condition as i <= N.
    3. Add increment statement as i++ or i+=1.
    4. Initialize a variable sum with 0.
    5. Start adding i with the sum at each iteration of for loop and print i.
    6. 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 = 55

    Time Complexity: O(n)

    Alternate Approach

    1. Start for loop initialization with i = 1.
    2. Write testing condition as i <= N.
    3. Add increment statement as i++ or i+=1.
    4. Start Printing i for each iteration.
    5. 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 = 15

    Time Complexity: O(n)

    Auxiliary Space: O(1) as it is using constant space for variables


    How to print sum of even number in Java?

    int sum = 0; for (int i = 1; i <= 10; i++) { // Logic for sum of EVEN if (i % 2 == 0) { sum = sum + i; } } System. out. println("Sum of all even from 1 to 10 is: " + sum);

    How to print n natural number in Java?

    Java Program to Print First N Natural Numbers using Recursion.
    import java.util.Scanner;.
    public class Natural..
    public static void main(String[] args).
    int n;.
    Scanner s = new Scanner(System. in);.
    System. out. print("Enter any number:");.

    How to display sum in Java?

    Sum of Two Numbers Using Command Line Arguments in Java.
    public class SumOfNumbers4..
    public static void main(String args[]).
    int x = Integer.parseInt(args[0]); //first arguments..
    int y = Integer.parseInt(args[1]); //second arguments..
    int sum = x + y;.
    System.out.println("The sum of x and y is: " +sum);.

    How to print sum of odd and even numbers in Java?

    Java Program to Find the Sum of Even and Odd Numbers.
    import java.util.Scanner;.
    public class Sum_Odd_Even..
    public static void main(String[] args).
    int n, sumE = 0, sumO = 0;.
    Scanner s = new Scanner(System. in);.
    System. out. print("Enter the number of elements in array:");.