The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Program to Calculate the Sum of Natural Numbers
Natural numbers are the numbers 1, 2, 3, 4… which are also called counting numbers. These are basically whole numbers except 0.
We can find the sum of natural numbers in Java using the following methods-
- Sum of natural numbers using while loop
- Sum of natural numbers using for loop
- Sum of natural numbers using formula
- Sum of natural numbers using recursion
- Sum of natural numbers between a range using an user defined function(do-while loop)
Method-1 : Sum of natural numbers using while loop
We will be taking the last element as input from the user and run the while loop while our counter is less than the number.
Let’s see the below program to understand it more clearly.
import java.util.Scanner; class sumNatural { public static void main(String args[]) { System.out.println("Enter the last element"); //Taking input from user Scanner scan = new Scanner(System.in); int num = scan.nextInt(); int i=0, sum=0; while(i<=num) { //Iterating the counter for each iteration //and adding the value to the previously existing sum. sum+=i; i++; } System.out.println("The sum of natural numbers including "+num+" is "+sum); } }
Output: Enter the last element 50 The sum of natural numbers including 50 is 1275
Method-2 : Sum of natural numbers using for loop
Like the previous example we will be taking the last element as input from the user and run the for loop but, here we won’t have to use another iterator. The for loop iterator will do the work.
Let’s see the below program to understand it more clearly.
import java.util.Scanner; class sumNatural { public static void main(String args[]) { System.out.println("Enter the last element"); //Taking input from user Scanner scan = new Scanner(System.in); int num = scan.nextInt(); int sum=0; for(int i=1;i<=num;i++) { //adding the iterator valueto the prevously existing sum. sum+=i; } System.out.println("The sum of natural numbers including "+num+" is "+sum); } }
Output: Enter the last element 50 The sum of natural numbers including 50 is 1275
Method-3 : Sum of natural numbers using formula
We can also find the sum of natural numbers using the math formula.
Sum = n(n+1)/2;
Let’s see the below program to understand it more clearly.
import java.util.Scanner; class sumNatural{ public static void main(String args[]) { System.out.println("Enter the last element"); //Taking input from user Scanner scan = new Scanner(System.in); int num = scan.nextInt(); //Using the math formula to calculate the sum int sum=num*(num+1)/2; System.out.println("The sum of natural numbers including "+num+" is "+sum); } }
Output: Enter the last element 50 The sum of natural numbers including 50 is 1275
Method-4 : Sum of natural numbers using recursion
We can find the sum of natural numbers using recursion inside an user defined function.
import java.util.Scanner; class sumNatural { public static int sumNumbers(int num) { //Recursive function if (num != 0) return num + sumNumbers(num - 1); else return num; } public static void main(String args[]) { System.out.println("Enter the last element"); //Taking input from user Scanner scan = new Scanner(System.in); int num = scan.nextInt(); //Using recursive function to calculate the sum System.out.println("The sum of natural numbers including "+num+" is "+sumNumbers(num)); } }
Output: Enter the last element 50 The sum of natural numbers including 50 is 1275
Method-5 : Sum of natural numbers between a range using an user defined function(do-while loop)
We can find the sum of a particular range using a do-while loop inside a user-defined function. We have to set the counter to the lower limit of the range.
import java.util.Scanner; class sumNatural { public static int sumNumbers(int lowerRange, int upperRange) { int sum = 0; do{ sum+=lowerRange++; }while(lowerRange!=upperRange); return sum; } public static void main(String args[]) { int lowerRange = 10, upperRange = 90; System.out.println("The sum of natural numbers from "+lowerRange+" and "+upperRange+" is "+sumNumbers(lowerRange,upperRange)); } }
Output: The sum of natural numbers from 10 and 90 is 3960
Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.
Related Java Decision Making and Loop Programs:
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)