Dark Mode On/Off

# Java Program To Calculate the sum of natural numbers

All the positive numbers starting from 1 to n, i.e., 1,2,3,....,n is known as a natural number. The sum of all these numbers gives us the sum of natural numbers.

Here, we are given a number and our task is to calculate the sum of all natural numbers.

Input: Enter the numbers: 1 2 3 4 5

Output: Sum of all the numbers: 15

## Program 1: Sum of Natural Numbers

In this method, we will use the for loop to calculate the sum of natural numbers.

### Algorithm

1. Start
2. Declare the number.
3. Initialize the number.
4. Use a for loop to calculate the sum.
5. Declare a sum variable and initialize it to 0.
6. Update the value of that variable in each iteration.
7. Print the final value.
8. Stop.

Below is the code for the same.

The below program demonstrates how to use a for loop to calculate the sum of natural numbers.

``````//Java Program to calculate the sum of natural numbers
import java.util.Scanner;

public class CalculateSum
{

// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int num; //Declare the number
System.out.println("Enter the number");
num=sc.nextInt();    //Initialize the number

int sum=0;   //Variable to calculate the sum
for(int i=1; i<=num;i++)
{
sum=sum+i;
}
System.out.println("The sum of natural numbers is "+sum);
}
}``````

Enter the number 5
The sum of natural numbers is 15

## Program 2: Sum of Natural Numbers

In this method, we will use the while loop to calculate the sum of natural numbers.

### Algorithm

1. Start
2. Declare the number.
3. Initialize the number.
4. Use a while loop to calculate the sum.
5. Declare a sum variable and initialize it to 0.
6. Update the value of that variable in each iteration.
7. Print the final value.
8. Stop.

Below is the code for the same.

The below program demonstrates how to use a while loop to calculate the sum of natural numbers.

``````//Java Program to calculate the sum of natural numbers
import java.util.Scanner;

public class CalculateSum
{

// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int num; //Declare the number
System.out.println("Enter the number");
num=sc.nextInt();    //Initialize the number
int i=1;
int sum=0;   //Variable to calculate the sum
while(i <= num)
{
sum += i;
i++;
}
System.out.println("The sum of natural numbers is "+sum);
}
}``````

Enter the number 10
The sum of natural numbers is 55

## Program 3: Sum of Natural Numbers

In this method, we will use a mathmatical formula to calculate the sum of natural numbers.

### Algorithm

1. Start
2. Declare the number.
3. Initialize the number.
4. Use the formula to calculate the sum.
5. Declare a sum variable that will store the final sum.
6. Use the formula sum= num*(num+1)/2
7. Print the final value.
8. Stop.

Below is the code for the same.

The below program demonstrates how to use the formula to calculate the sum of natural numbers.

``````//Java Program to calculate the sum of natural numbers
import java.util.Scanner;

public class CalculateSum
{

// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int num; //Declare the number
System.out.println("Enter the number");
num=sc.nextInt();    //Initialize the number

//formula to calculate the sum of natural numbers
int sum= num *( num +1) / 2;

System.out.println("The sum of natural numbers is "+sum);
}
}``````

Enter the number 18
The sum of natural numbers is 171

## Program 4: Sum of Natural Numbers

In this method, we will use the function to calculate the sum of natural numbers.

### Algorithm

1. Start
2. Declare the number.
3. Initialize the number.
4. Call a function that will calculate the sum of natural numbers.
5. Declare a sum variable that will store the final sum.
6. Use the formula sum= num*(num+1)/2 and return the sum.
7. Print the final value.
8. Stop.

Below is the code for the same.

The below program demonstrates how to use a function to calculate the sum of natural numbers.

``````//Java Program to calculate the sum of natural numbers
import java.util.Scanner;

public class CalculateSum
{
static int sumOfNaturalNumbers(int n)
{
//formula to calculate the sum of natural numbers
return n * (n + 1) / 2;
}

// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int num; //Declare the number
System.out.println("Enter the number");
num=sc.nextInt();    //Initialize the number

int sum=sumOfNaturalNumbers(num);

System.out.println("The sum of natural numbers is "+sum);
}
}``````

Enter the number 12
The sum of natural numbers is 78

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.