# Find Sum of N Natural Numbers in Java

In this blog, we will explore three different methods to find the sum of natural numbers in Java. We will discuss the significance of this problem and the relevance of Java in solving it. Whether you’re a seasoned developer or a beginner in Java programming, understanding different approaches to solving this problem is essential.

## What does it mean to find the sum of N Natural Numbers in Java?

In the programming world, one frequently encounters the need to find the sum of natural numbers.

Finding the sum of the first N natural numbers in Java involves adding all positive integers from 1 to N. Additionally the formula gives the sum of the first N natural numbers as:

Sum = N * (N + 1) / 2

Here, N denotes the quantity of natural numbers that we will add. The sum is calculated using the arithmetic series formula, which provides a quick and efficient way to find the sum of consecutive numbers.

Understand the Concept of Smallest of Three Numbers in Java, Here!

### N Natural Number Examples

Let’s understand N Natural Number in Java examples:

1. Sum of first 5 natural numbers:

N = 5

Sum = 5 * (5 + 1) / 2

Sum = 5 * 6 / 2

Sum = 30 / 2

Sum = 15

2. Sum of first 10 natural numbers:

N = 10

Sum = 10 * (10 + 1) / 2

Sum = 10 * 11 / 2

Sum = 110 / 2

Sum = 55

3. Sum of first 20 natural numbers:

N = 20

Sum = 20 * (20 + 1) / 2

Sum = 20 * 21 / 2

Sum = 420 / 2

Sum = 210

In Java, you can use the formula mentioned above to find the sum of the first N natural numbers efficiently. By plugging the value of N into the formula, you can calculate the sum in constant time, regardless of the size of N. This makes it an ideal approach, particularly for finding the sum of large numbers of natural numbers.

## Find Sum of Natural Numbers Using Java for Loop

// Find Sum of Natural Numbers in Java
public class SumOfNaturalNumEx
{
public static void main(String[] args)
{
int i, n = 10, sum = 0;
//executes until the condition returns true
for(i = 1; i <= n; ++i)
{
//adding the value of i into sum variable
sum = sum + i;
}
//prints the sum
System.out.println("Sum of First 10 Natural Numbers is = " + sum);
}
}

Also, learn about Largest of Three Numbers in Java, Now!

#### Explanation of the code:

Approach:
Java code illustrates a simple and efficient approach to find the sum of the first 10 natural numbers.

The code uses a for loop to iterate through the numbers from 1 to 10 and moreover adds each number to the variable “sum.” By the end of the loop, the variable “sum” holds the sum of the first 10 natural numbers.

We’ll examine the code in detail to understand its mechanics.

1. The code initializes variables “i” (used as a loop counter), “n” (the value of N, which is 10 in this case), and “sum” (initialized to 0 to store the sum).

2. The for loop is used to iterate from 1 to “n” (10 in this case). During each iteration, we add the value of “i” to the current sum.

3. Once the loop completes, the final value of “sum” holds the sum of the first 10 natural numbers.

4. The code then prints the result using System.out.println().

On the whole the code efficiently calculates the sum of the first 10 natural numbers and displays it as output.

#### Output:

Sum of First 10 Natural Numbers is = 55

## Java Program to find Sum of Natural Numbers Using Java while Loop

// Find Sum of Natural Numbers in Java
public class SumOfNaturalNumEx2
{
public static void main(String[] args)
{
int n = 100, i = 1, sum = 0;
//executes until the condition returns true
while(i <= n)
{
//adding the value of i into sum variable
sum = sum + i;
//increments the value of i by 1
i++;
}
//prints the sum
System.out.println("Sum of First 100 Natural Numbers is = " + sum);
}
}

#### Explanation of the code:

Below Java code calculates the sum of the first 100 natural numbers using a while loop. Here’s a comprehensive explanation of the code’s functioning:

1. The code initializes variables `n`, `i`, and `sum`. The variable ‘n’ is assigned a value of 100, representing the count of natural numbers to add. `i` is initialized to 1, and furthermore `sum` is set to 0, which will store the final sum.

2. We use the while loop to iterate through the numbers from 1 to ‘n’. It runs as long as the condition `i <= n` is true.

3. Inside the loop, the value of `i` is added to the `sum` variable in each iteration. The loop then increments the value of `i` by 1 using `i++`.

4. The loop continues until `i` reaches the value of `n`, adding each natural number to the `sum`.

5. Once the loop ends, the program prints the final sum using `System.out.println()`.

In this code, the while loop efficiently calculates the sum of the first 100 natural numbers and displays it as output.

#### Output:

Sum of First 100 Natural Numbers is = 5050

## Java Program to Find Sum of n Natural Numbers

import java.util.Scanner;
public class SumOfNaturalNumEx3
{
public static void main(String[] args)
{
int n, i, sum = 0;
//object of Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Sum from: ");
//takes an integer as input
i = sc.nextInt();
System.out.print("Sum up to: ");
//takes an integer as input
n = sc.nextInt();
while(i <= n)
{
//adding the value of i into sum variable
sum = sum + i;
//increments the value of i by 1
i++;
}
//prints the sum
System.out.println("Sum of Natural Numbers = " + sum);
}
}

#### Explanation of the code:

The Java code provided calculates the sum of natural numbers from a starting point ‘i’ to an ending point ‘n’. It takes user input for the starting and ending values using the Scanner class and then uses a while loop to iterate through the range of natural numbers and add them to the ‘sum’ variable.

Now, we’ll dissect how the code operates step by step:

1. The code declares integer variables ‘n’, ‘i’, and ‘sum’, where ‘n’ represents the ending point, whereas ‘i’ represents the starting point, and ‘sum’ holds the sum of natural numbers.

2. We create the Scanner object ‘sc’ to read user input from the console.

3. The user is prompted to enter the starting point ‘i’ and then the ending point ‘n’ for the sum of natural numbers.

4. The while loop is used to iterate through the natural numbers from ‘i’ to ‘n’. In each iteration, the value of ‘i’ is added to the ‘sum’ variable.

5. After adding all the numbers from ‘i’ to ‘n’, the while loop terminates, and the program prints the result, displaying the sum of natural numbers from ‘i’ to ‘n’.

In Java programming, this code efficiently calculates the sum of natural numbers within the given range moreover demonstrates the use of user input and loops.

Get a Complete list of Online Certification Courses in India here!

#### Output:

Sum from: 10
Sum up to: 100
Sum of Natural Numbers = 5005

## Java Program To Find Sum Of Natural Numbers Using Function

import java.util.Scanner;
public class SumOfNaturalNumEx4
{
//method that returns the sum of n natural numbers
static int naturalNumSum(int num)
{
int sum = 0;
//executes until the condition becomes false
for (int i = 1; i <= num; i++)
//adding the value of i to the sum variable
sum = sum + i;
return sum;
}
//main method
public static void main(String args[])
{
int num = 50;
//calling method and prints the sum
System.out.println("Sum of Natural Numbers from 1 to 50 is: "+naturalNumSum(num));
}
}

#### Explanation of the code:

A for loop in Java calculates the sum of the first N natural numbers. Here’s an explanation of how the code works:

1. First the code defines a class named “SumOfNaturalNumEx4” and then declares a static method named “naturalNumSum” to calculate the sum of N natural numbers.

2. The method “naturalNumSum” accepts an integer input “num,” which represents the count of natural numbers to add.

3. Inside the “naturalNumSum” method, an integer variable “sum” is initialized to store the sum of natural numbers.

4. The for loop is used to iterate from 1 to “num”. During each iteration, the “sum” variable accumulates the value of “i.”

5. After the for loop completes, furthermore, the “sum” variable contains the sum of the first N natural numbers.

6. In the main method, the variable “num” is assigned the value 50, hence representing the sum of natural numbers from 1 to 50.

7. The “naturalNumSum” method is called with “num” as an argument, and consequently, the result is printed as “Sum of Natural Numbers from 1 to 50 is: “.

This code efficiently calculates the sum of N natural numbers using a simple for loop, furthermore making it an optimal solution for finding the sum of large numbers of natural numbers.

#### Output:

Sum of Natural Numbers from 1 to 50 is: 1275

In this blog, consequently we explored three different methods to find the sum of natural numbers in Java. We discussed the significance of this problem and then efficiently solved it using arithmetic series formulas, loops, and functions. Understanding these approaches will equip you to handle similar problems in interviews and real-world scenarios.

We hope you found our blog on “First Sum of Natural Numbers in Java” both informative and helpful. For more programming-related blogs and courses, visit our website Newtum and keep learning with us!