4 different approaches to find sum of natural number in java

In this article you get 4 different approaches to find the sum of natural number using java programming language. codeYourslf blog, basic of java
4 min read

Sum of natural number in java (4 ways)

4 different approaches to find sum of natural number in java

In this article you get 4 different approaches to find the sum of natural number using java programming language.

Approach :- 1 "Sum of natural number using for loop"

package CodeYourslf.allPrograms.sumNaturalNumber;

import java.util.Scanner;

public class byForLoop {

    public static void main(String[] args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the number: ");

      int num = sc.nextInt();

      

      int sum = 0;

      for(int i = 0; i <=num; i++){

        sum = sum + i;

      }

      

      System.out.println("The sum is: "+sum);

    }

}

Output

4 different approaches to find sum of natural number in java

Approach :- 2 "Sum of natural number using while loop"

package CodeYourslf.allPrograms.sumNaturalNumber;

import java.util.Scanner;

public class byWhileLoop {

    public static void main(String[] args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the number: ");

      int num = sc.nextInt();

      

      int sum = 0;

      int i = 1;

      while(i <= num){

        sum = sum + i;

        i++;

      }

      

      System.out.println("The sum is: "+sum);

    }

}

 

Approach :- 3 "sum of natural number using formula"

 package CodeYourslf.allPrograms.sumNaturalNumber;

import java.util.Scanner;

public class byFormula {

    public static void main(String[] args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the number: ");

      int num = sc.nextInt();

      

      int sum = (num*(num + 1))/2;

      System.out.println("The sum is: "+sum);

    }

}

Approach :- 4 "sum of natural number using method"

package CodeYourslf.allPrograms.sumNaturalNumber;

import java.util.Scanner;

public class byMethod {

    static int sum(int num){

      return num*(num +1 )/2;

    }

    public static void main(String[] args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the number: ");

      int num = sc.nextInt();

      

      int result = sum(num);

      System.out.println("The sum is: "+result);

    }

}

 

Details Explanation 

Here I will explain how these above approaches are working.

- Natural number means the positive integers i.e 1, 2, 3, 4....... upto infinity.

Sum of natural number means addition of all the number from 1 to that given number.

Ex :- sum of 10 number means.

  • 1+2+3+.....+9+10 = 55.

Approach:-1

In this approach I have started the for loop from 1 upto that number and I will update the sum at each iteration by sum = sum + i;

Approach :- 2

Here I have used while loop to find out the sum of natural number in java programming. 

Approach :- 3

In approach 3, I have used the formula to find the sum of natural numbers.

- The formula is : 

          Sum = num*(num + 1) / 2;

Approach :- 4

Here I used the method concept to find the sum of natural number.

👉 Please go through the above mentioned source code and if you get any doubt, please feel free write it down on comment below.

😊 Hey if you learn something new from this article, please share with your friends.


`

You may like these posts

1 comment

  1. second ago
    Please give your feedback
Hey Please 👏, feel free to share your opinion 🌍