The natural numbers comprise all positive integers starting from 1 and extending to infinity. They form a sequence of numbers represented by 1, 2, 3, 4, 5, and so on, denoted as n. When we sum these natural numbers, we obtain the sum of the natural numbers.
In this section, we will develop three Java programs to compute the sum of natural numbers:
- Java program to find the sum of the first 100 natural numbers: This program will calculate the sum of the first 100 natural numbers by iterating from 1 to 100 and adding each number to the sum.
- Java program to find the sum of n natural numbers: This program will determine the sum of n natural numbers, where the value of n will be provided by the user. It will use a loop to iterate from 1 to n and add each number to the sum.
- Java program to find the sum of n natural numbers using a function: This program will utilize a function to calculate the sum of n natural numbers. The function will take the value of n as a parameter and use a loop to iterate from 1 to n, adding each number to the sum.
Formula: Sum of n natural numbers=n*(n+1)/2
To find the sum of the first 100 natural numbers, we can apply the formula provided above.
sum = (n * (n + 1)) / 2
By substituting n with 100, we obtain:
sum = (100 * (100 + 1)) / 2
Simplifying further:
sum = (100 * 101) / 2
sum = 5050
In this section, we will explore different approaches to finding the sum of natural numbers using Java. We will cover the following methods:
- Using Java for Loop
- Using Java while Loop
- Using Function
Find the Sum of Natural Numbers using Java for Loop
SumOfNaturalNumber.java
public class SumOfNaturalNumber { public static void main(String[] args) { int i, num = 10, sum = 0; //executes until the condition returns true for(i = 1; i <= num; ++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); } }
Output:
Sum of First 10 Natural Numbers is = 55
Find the Sum of Natural Numbers using Java while Loop
In the following example, we have modified by replacing the for loop with a while loop. The while loop continues executing until the condition “i <= num” becomes false. By employing the while loop, we calculate the sum of natural numbers up to a specified limit.
SumOfNaturalNumber1.java
public class SumOfNaturalNumber1 { public static void main(String[] args) { int num = 100, i = 1, sum = 0; //executes until the condition returns true while(i <= num) { //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); } }
Output:
Sum of First 100 Natural Numbers is = 5050
The sum of n Natural Numbers
The following program is designed to find the sum of natural numbers within a specified range. It employs a while loop, similar to the previous program discussed. Additionally, it takes two inputs from the user: “i” and “num”. The variable “i” represents the starting number, while “num” represents the end number of the range.
By using this program, we can compute the sum of natural numbers starting from the value of “i” and continuing until the value of “num”. For instance, if we want to find the sum of natural numbers from 20 to 100, we would provide 20 as the value of “i” and 100 as the value of “num”.
SumOfNaturalNumber2.java
import java.util.Scanner; public class SumOfNaturalNumber2 { public static void main(String[] args) { int num, 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 num = sc.nextInt(); while(i <= num) { //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); } }
Output:
Sum from: 1 Sum up to: 1000 Sum of Natural Numbers = 500500
Find the Sum of Natural Numbers using Function
The following program demonstrates how to calculate the sum of n natural numbers using a function.
SumOfNaturalNumber3.java
public class SumOfNaturalNumber3 { //method that returns the sum of n natural numbers static int naturalNumberSum(int n) { int sum = 0; //executes until the condition becomes false for (int i = 1; i <= n; i++) //adding the value of i to the sum variable sum = sum + i; return sum; } //main method public static void main(String args[]) { int n = 50; //calling method and prints the sum System.out.println("Sum of Natural Numbers is: "+naturalNumberSum(n)); } }
Output:
Sum of Natural Numbers is: 1275
SumOfNaturalNumber4.java
public class SumOfNaturalNumber4 { //method that returns the sums static int sumOfNaturalNumbers(int n) { //formula to calculate the sum of natural numbers return n * (n + 1) / 2; } //main method public static void main(String args[]) { int n = 5; //calling the method and printing the sum System.out.println("Sum of Natural Numbers is: "+sumOfNaturalNumbers(n)); } }
Output:
Sum of Natural Number is: 20100
In conclusion, the provided Java program effectively calculates the sum of natural numbers using various approaches. The program demonstrates the use of loops, such as the while loop or for loop, and mathematical formulas to compute the sum.
Follow tutorials.freshersnow.com to learn more.