- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Find Sum of Natural Numbers Using While Loop
Sum of Natural Numbers can be calculated using different Iterative Statements in Programming Languages. Iterative Statements are statements which execute a particular set of code lines until the condition in the loop statement is failed. In this article, we will discuss how to calculate Sum of Natural Numbers using a while- loop which is an iterative statement in Java Programming Language.
Sum of Natural Numbers
The sum of natural numbers generally indicates the total sum of elements from 1 to n. Mathematically it can be represented as follows
Sum of n Natural Numbers = 1+2+3+.........+(n-2) + (n-1) + n
Example: Find sum of 5 natural numbers.
Input = 5 Output = 15
Explanation: sum of natural numbers from 1 to 5 = 1+ 2+ 3+ 4+ 5 = 15.
While Statement in Java
A while loop in Java language is one of the iterative statements present which allows a set of code block to be executed repeatedly until the condition becomes false.
Syntax
initilaze condition variable while (condition) { // statements Update condition variable; }
Here is a code snippet for the while loop in the 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 to Find Sum of Natural Numbers
STEP 1 − Initialize three variables which denote the number of natural numbers to find sum, a counter variable, a variable which stores the sum of natural numbers.
STEP 2 − Use the while and perform the addition of sum of natural numbers until ‘n’.
STEP 3 − Print the total sum of natural numbers.
Example
In this below we use while loop in java to find the sum of natural numbers. We declared a variable n for getting sum of up to n numbers. The ‘i’ is the counter variable used. We use the while loop and iterate from i to n and sum up all the values and store in sum variable. At last the value of sum variable to get the output.
// 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)
Thus, in this article we have learned how to use the while - loop concept in java programming language to calculate the sum of n natural numbers.