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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The average of the squares of natural numbers is calculated by adding all the squares of n natural numbers and then dividing by that number. The first two natural numbers in the example are 2.5, 12+22=5=>5/2=2.5. There are two methods of calculation in programming - Using loops Using formulas Calculating the average of squares of natural numbers using loops This logic works by finding the squares of all natural numbers. Find the square of each by looping from 1 to n and add to the sum variable. Then divide that sum by n. Program to calculate the sum of squares of natural numbers - sample code real-time demonstration #include<stdio.h>intmain(){ intn=2;

Title: Using while loops to implement string splicing in PHP In the PHP language, using while loop statements to implement string splicing is a common operation. Loop through an array, list, or other data source, concatenating each element or value into a string. This method is useful when dealing with large amounts of data or when strings need to be generated dynamically. Let's look at some specific code examples below. First, we prepare an array as the data source, and then use a while loop to implement string splicing

The purpose of a loop is to repeatedly execute a certain piece of code. Using loops can reduce programming pressure, avoid code redundancy, improve development efficiency, and facilitate later maintenance. The while loop is the simplest loop statement provided in JavaScript. Let's learn about the use of while loops and do-while loops.

Introduction to Java program to calculate the sum of numbers in a list using while loop is a simple program that takes a list of integers and calculates their sum using while loop structure. In this program, an ArrayList of integers is created and some numbers are added to the list. The program then uses a while loop to iterate through each element in the list, adding each element to the variable "sum", which keeps track of the running sum of the numbers. After the loop completes, the final value of "sum" is printed to the console, which is the sum of all the numbers in the list. This program demonstrates a common technique for working with data collections in programming, which is to use a loop to iterate over each element in the collection and perform some calculation or transformation on each element. The plan also focuses on

Problem Enter a sentence at runtime and write a code to calculate the average length of words appearing in the sentence Solution algorithm STARTStep1:declarecharacter,intanddoublevariablesStep2:EnteranystatementStep3:whileloop Checkconditionstmt[i]=getchar())!=''  

The difference is: 1. The while loop judges the condition first and then executes the loop body, while the do-while loop executes the loop body first and then judges the condition; 2. The while loop judges the loop condition first, and if the condition is met, the loop body is executed. code, and then judge the condition again, and loop like this until the loop is jumped out when the condition is not satisfied. The do-while loop first executes the code in the loop body, and then judges whether the loop condition is satisfied. If the condition is satisfied, the loop continues to execute. The code in the body loops like this until the loop is jumped out when the condition is not met.

There are two syntaxes for implementing while loop statements in PHP, namely: 1. "while (...) {...}" syntax, which means that as long as the specified condition is true, the while loop will execute the code block; 2. " do {...} while (...);" syntax, which executes the code block once, then checks the condition, and repeats the loop if the specified condition is true.

Sum of the cubes of the first n natural numbers is a program that adds the cubes of all natural numbers. It is the sum of the series 1^3+2^3+...+n^3, which is the sum of cubes of n natural numbers. Input:6Output:441Explanation1^3+2^3+3^3+4^3+5^3+63=441Use a For loop to increase the number and cube it, then take their sum. Example #include<iostream>usingnamespacestd;intmain(){ intn=6; &
