Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Java program to Sum of natural numbers 1 to n |5 ways

Posted on October 19, 2019October 19, 2019

Table of Contents

  • Java program to Sum of natural numbers 1 to n| in 5 ways
    • Program to Find sumĀ  of natural numbers – using for loop
    • Program to find sum of natural numbers – using the while loop
    • Program to find sum of natural numbers – using the do-while loop
    • Program to find sum and average of natural numbers – using the method
    • Program to find sum and average of natural numbers – using the recursive method

Java program to Sum of natural numbers 1 to n| in 5 ways

In this tutorial, we will discuss the Java program to the sum of Natural number from 1 to n

Java program to sum of natural numbers from 1 to n
Addition of Natural number

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

Program to Find sumĀ  of natural numbers – using for loop

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

Program to find sum of natural numbers – using the while loop

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

Program to find sum of natural numbers – using the do-while loop

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

Program to find sum and average of natural numbers – using the method

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

Program to find sum and average of natural numbers – using the recursive method

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

for lop in Java language

while loop in Java language

do-while loop in Java language

method 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

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes