Home  >  Article  >  Java  >  Java program to find the sum of natural numbers using while loop

Java program to find the sum of natural numbers using while loop

王林
王林forward
2023-09-02 09:53:061322browse

Java program to find the sum of natural numbers using while loop

The sum of natural numbers can be calculated using different iteration statements in programming languages. An iteration statement is a statement that executes a specific set of lines of code until a condition in the loop statement fails. In this article, we will discuss how to calculate the sum of natural numbers using while loop (iteration statement in Java programming language).

Sum of natural numbers

The sum of natural numbers generally represents the sum of elements from 1 to n. Mathematically it can be expressed as follows

Sum of n Natural Numbers =  1+2+3+.........+(n-2) + (n-1) + n                           

Example: Find the sum of 5 natural numbers.

Input = 5
Output = 15

Explanation: The sum of the natural numbers from 1 to 5 = 1 2 3 4 5 = 15.

While statement in Java

The while loop in Java language is an iterative statement that allows a set of code blocks to be executed repeatedly until a condition becomes false.

grammar

initilaze condition variable
while (condition)
{
   // statements
   Update condition variable;
}

This is the code snippet of while loop in Java.

int i = 0;  // initialzing  the condition variable

While ( i <  10 ) {
   System.out.println("tutor");   
   // statement which is executed untill condition fails
   i++;
   // updating the condition variable
}

Algorithm for finding the sum of natural numbers

Step 1 - Initialize three variables, representing the number of natural numbers to be summed, a counter variable, and a variable storing the sum of natural numbers.

Step 2 - Use while and perform addition of sums of natural numbers up to "n".

Step 3 - Print the sum of the natural numbers.

Example

Below we use the while loop in java to find the sum of natural numbers. We declare a variable n to get the sum of up to n numbers. "i" is the counter variable used. We use while loop to iterate from i to n and sum all values ​​and store in sum variable. Finally, the output is obtained through the value of the sum variable.

// Java program to calculate sum of n natural numbers using the concept of While - loop
import java.util.*;
public class Main {
   public static void main(String[] args) {
      int n= 7,  i = 1,sumofDigits = 0;
      while (i <= n) { 
         // checking the condition if it satisfies then the statements inside loop are executed
         sumofDigits = sumofDigits + i; 
         // performing addition to sum as the number should be added to sum
         i++;
         // Incrementing  the variable used in condition
      }
      System.out.println("Sum of natural numbers up to 7 is :" +sumofDigits);
   }
}

Output

Sum of natural numbers up to 7 is :28

Time complexity: O(N) Auxiliary space: O(1)

So, in this article we learned how to calculate the sum of n natural numbers using the while loop concept in java programming language.

The above is the detailed content of Java program to find the sum of natural numbers using while loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete