Table of Contents
In this tutorial, we will discuss the Java program to the sum of Natural number from 1 to n
In this post, we are going to learn how to find the sum of natural numbers in Java language in different 5 ways
Program 1
import java.util.Scanner;
class Sum_Naturalfor{
public static void main(String args[]){
int sum=0,num;
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.print("Enter the integer number: ");
num=scan.nextInt();//get input from the user for num1
for(int i=1; i<=num; i++){
sum+=i; //sum=sum+i;
}
System.out.print("Sum of natural numbers: "+sum);
}
}
When the above code is executed it produces the following output
Enter the integer number: 12 Sum of natural numbers :78
This program allows the user to enter a maximum number. and it displays the sum of natural numbers from 1 to given number using for loop in Java language
Program 2
import java.util.Scanner;
class Sum_Naturalwhile{
public static void main(String args[]){
int sum=0,num;
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.print("Enter the integer number: ");
num=scan.nextInt();//get input from the user for num1
int i=1;
while( i<=num){
sum+=i; //sum=sum+i;
i++;
}
System.out.print("Sum of natural numbers: "+sum);
}
} When the above code is executed it produces the following output
Enter the integer number: 25 Sum of natural numbers: 325
This program allows the user to enter a maximum number. and it displays the sum of natural numbers from 1 to given number using while loop in Java language
Program 3
import java.util.Scanner;
class Sum_NaturalDowhile{
public static void main(String args[]){
int sum=0,num;
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.print("Enter the integer number: ");
num=scan.nextInt();//get input from the user for num1
int i=1;
do{
sum+=i; //sum=sum+i;
i++;
}while( i<=num);
System.out.print("Sum of natural numbers: "+sum);
}
} When the above code is executed it produces the following output
Enter the integer number: 50 Sum of natural numbers: 1275
This program allows the user to enter a maximum number. and it displays the sum of natural numbers from 1 to given number using the do-while loop in Java language
Program 4
import java.util.Scanner;
class Sum_Naturalmethod{
public static void main(String args[]){
int sum=0,num;
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.print("Enter the integer number: ");
num=scan.nextInt();//get input from the user for num1
sum=sumofNaturalNum(num);
System.out.println("The sum of Natural number fron 1 to "+num+": "+sum);
}
public static int sumofNaturalNum(int n){
if(n==0)
{
return n;
}
else{
return(n*(n+1)/2);
}
}
} When the above code is executed it produces the following output
Enter the integer number: 15 The sum of Natural number from 1 to 15: 120
This program allows the user to enter a maximum number. and it displays the sum of natural numbers from 1 to given number using the method in Java language
Program 5
import java.util.Scanner;
class Sum_Naturalrecmethod{
public static void main(String args[]){
int sum=0,num;
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.print("Enter the integer number: ");
num=scan.nextInt();//get input from the user for num1
sum=sumofNaturalNum(num);
System.out.println("The sum of Natural number fron 1 to "+num+": "+sum);
}
public static int sumofNaturalNum(int n){
if(n==0)
{
return n;
}
else{
return(n+sumofNaturalNum(n-1));
}
}
} When the above code is executed it produces the following output
Enter the integer number: 30 The sum of Natural number from 1 to 30: 465
This program allows the user to enter a maximum number. and it displays the sum of natural numbers from 1 to the given number using the recursive method
Suggested for you
do-while loop in Java language
recursion in Java language
Similar post
C++ program to sum of Natural number from 1 to n
C program to sum of Natural number from 1 to n
Python program to Sum of natural numbers 1 to n
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.