Java Program to Find Sum of Natural Numbers Using While Loop Last Updated : 14 Jun, 2022 Comments Improve Suggest changes Like Article Like Report While loop arises into play where beforehand there is no conclusive evidence that how many times a loop is to be executed. This is the primary reason as there is no strict tight bound over how many numbers the sum is to be evaluated. Situations in which the output can be displayed using test condition abiding hardcoded outputs by simply running the programs, while the loop is taken for consideration. Syntax: while (test_expression) { // statements update_expression; } Sum: The sum of natural numbers from '1' to 'n' can be mathematically written where n represents the number of numbers entered by the user or to be evaluated. Using the principle of mathematical induction above formula equals: 1 + 2 + 3 + 4 + 5 + ...+ (n-2) + (n-1) + n = [n(n+1)]/2 Illustration: Suppose the sum of 10 natural numbers is to be calculated then by above formula 55 should be the output. Input : 5 Processing : 1 + 2 + 3+ 4 + 5 Output : 15 Approach: Using a While loop where a condition is passed as a parameter in a while statement which is referred to as a 'test condition'. Test Expression: In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to update expression. Otherwise, we will exit from the while loop.Example: i ≤ 10Update Expression: After executing the loop body, this expression increments/decrements the loop variable by some value.Example: i++; Algorithm: for the sum of natural numbers using while loop is as follows Initializing n=10,sum=0,i=1; //where n is the number till the user want sumIf the natural number to be processed holds the test condition, compute the below steps, and if fails display the current sum as the final sum.The current sum is updated as the test condition holds true to the final sum.Increment the variable to move to the next natural number and if the test condition holds true, update the existing sum.Display the sumTerminate Implementation: Java // Java program to show sum of natural numbers // using the while loop import java.util.*; class GFG { public static void main(String[] args) { int n = 10, sum = 0, i = 1; /* While loop*/ // Test condition while (i <= n) { /* Statements to execute */ // Update the current sum till // test condition holds true sum = sum + i; // Increment the variable counter // or jumping to next natural number i++; } // Print the sum System.out.println( "Sum of natural numbers using while loop is:" + " " + sum); } } OutputSum of natural numbers using while loop is: 55 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Find Sum of Natural Numbers Using While Loop A abhijithoyur Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Compute the Sum of Numbers in a List Using While-Loop The task is to compute the sum of numbers in a list using while loop. The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion 2 min read Perfect Number Program in Java Using While Loop The number which is equal to the sum of its divisors is called a perfect number. Read the entered long number, assigned to the long variable n. While loop iterates until the condition (i<=n/2) is false. If the remainder of n/i=0 then add i value to the sum and increase the i value. After all the 2 min read Java Program to Find Sum of N Numbers Using Recursion Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved. To solve any problem using recursion, we should simply follow the below steps: Assume/Identify the smaller problem from the problem which is similar to the bigger/ori 4 min read Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop Problem Statement: The number is supposed to be entered by the user be it any random number lying within the primitive data-type holding the number. First, the number needs to be reversed. Secondary the sum of the number is to be calculated with the constraint to use a do-while loop. do-while loop: 6 min read Java Program to Compute the Sum of Numbers in a List Using For-Loop Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List 2 min read Java Program to Compute the Sum of Numbers in a List Using Recursion ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. I 5 min read Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve 3 min read Java Program to Add Two Numbers Given two integers num1 and num2, the task is to find the sum of the given two numbers in Java. Example of Addition of Two Numbers Input: A = 5, B = 6Output: sum = 11 Input: A = 4, B = 11 Output: sum = 15  Program to Add Two Numbers in Java Below is the implementation of adding two Numbers are ment 4 min read Java Program For Finding Subarray With Given Sum - Set 1 (Nonnegative Numbers) Given an unsorted array of nonnegative integers, find a continuous subarray which adds to a given number. Examples : Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Output: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10, 5 min read Java Program to Find Sum of Array Elements Given an array of integers. Write a Java Program to find the sum of the elements of the array. Examples: Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50 15 + 12 + 13 + 10 = 50 An array is a data structure that contains a group of elements. Typically th 3 min read Like