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

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

王林
Release: 2023-09-02 09:53:06
forward
1383 people have browsed it

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                           
Copy after login

Example: Find the sum of 5 natural numbers.

Input = 5
Output = 15
Copy after login

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;
}
Copy after login

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
}
Copy after login

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);
   }
}
Copy after login

Output

Sum of natural numbers up to 7 is :28
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template