Java Program to Find Sum of Natural Numbers Using While Loop Last Updated : 14 Jun, 2022 Summarize Comments Improve Suggest changes Share 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 Like