In this tutorial, we will discuss a concept of Java program to calculate the sum of natural numbers using loops
In this article, we are going to learn how to find the sum of natural numbers using loops in the Java programming language
Program 1
This program takes input from the user and stores in variable num. Then, the for loop is used to calculate the sum of natural numbers up to the given number.
import java.util.Scanner; class NaturalSum1{ public static void main(String args[]){ int sum=0; //variable declaration Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number to find sum: "); int num=scan.nextInt(); for(int i=1; i<=num; i++){ sum+=i; //sum=sum+i; } System.out.println("Sum of the numbers until "+num +":"+sum); //display result } }
When the above code is executed, it produces the following results
Enter the number to find sum: 15 Sum of the natural numbers until 15 : 120
Program 2
This program takes input from the user and stores in variable num. Then, the while loop is used to calculate the sum of natural numbers up to the given numbers.
import java.util.Scanner; class NaturalSum2{ public static void main(String args[]){ int sum=0; //variable declaration Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number to find sum: "); int num=scan.nextInt(); int i=1; while( i<=num){ sum+=i; //sum=sum+i; i++; } System.out.println("Sum of the numbers until "+num +":"+sum); } }
When the above code is executed, it produces the following results
Enter a number to find sum: 25 Sum of the numbers until 25: 325
Program 3
This program takes input from the user and stores in variable num. Then, the do-while loop is used to calculate the sum of natural numbers up to the given number.
import java.util.Scanner; class NaturalSum3{ public static void main(String args[]){ int sum=0; //variable declaration //1 Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number to calculate sum: "); int num=scan.nextInt(); //2 int i=1; do{ //3 sum+=i; //sum=sum+i; i++; } while( i<=num); System.out.println("Sum of the numbers until "+num +":"+sum); //4 } }
When the above code is executed, it produces the following results
Enter a number to calculate sum: 50 Sum of the numbers until 50: 1275
Method:
Similar post
C++ program to find the sum of natural numbers using loops
Python program to Calculate the sum of natural numbers using loops
C program to calculate the sum of natural numbers using loops
Suggested for you
do-while loop in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…