Sum of n numbers in Java
In this post, you will learn different Java programs to find the sum of n numbers.
In mathematics, the natural numbers are those numbers used for counting and ordering. Natural numbers are all positive numbers like 1, 2, 3, 4, and so on. They are the numbers you usually count and they continue till infinity.
The sum of n natural numbers is-
1 + 2 + 3 + .... + n
Sum of n numbers in Java using for loop
In the given Java program, we have used the for loop to calculate the sum of first 20 natural numbers.
public class SumOfNaturalNum
{
public static void main(String[] args)
{
int x, num = 20, sum = 0;
for(x = 1; x <= num; x++)
{
sum = sum + x;
}
//prints the sum
System.out.println("Sum of first 20 natural numbers: " + sum);
}
}
Output of the above code:
Sum of first 20 natural numbers: 210
Sum of n numbers in Java using while loop
In the given Java program, we have used while loop to calculate the sum of first 200 natural numbers.
public class SumOfNaturalNum
{
public static void main(String[] args)
{
int num = 200, x = 1, sum = 0;
while(x <= num)
{
sum = sum + x;
x++;
}
//prints the sum
System.out.println("Sum of first 200 natural numbers: " + sum);
}
}
Output of the above code:
Sum of first 200 natural numbers: 20100
Sum of n natural numbers in Java using formula
The mathematical formula to calculate the sum of natural numbers is-
sum= num*(num+1)/2
In the given Java program, we ask the user to provide the nth natural number and then use the above mathematical formula to calculate the sum of natural numbers.
import java.util.Scanner;
public class SumOfNaturalNumbers
{
// Driver method
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
int num;
System.out.println("Enter the number: ");
num=sc.nextInt();
// Formula to calculate
// the sum of natural numbers
int sum= num *( num +1) / 2;
System.out.println("The sum of first "+num+" natural numbers: "+sum);
}
}
Output of the above code:
Enter the number: 130
The sum of first 130 natural numbers: 8515
Sum of n numbers in Java using recursion
Recursion function is a function which is called by itself. In the given Java program, we call the recursion function to get the sum of n natural numbers.
import java.util.Scanner;
public class SumOfNaturalNumbers
{
private static Scanner sc;
public static void main(String[] args)
{
int num, sum = 0;
sc = new Scanner(System.in);
System.out.println("Enter the number: ");
num = sc.nextInt();
sum = SumOfNatNum(num);
System.out.println("The Sum of First "+ num + " Natural Numbers: " + sum);
}
public static int SumOfNatNum(int num)
{
if(num == 0)
{
return num;
}
else
{
return (num + SumOfNatNum(num - 1));
}
}
}
Output of the above code:
Enter the number: 112
The Sum of First 112 Natural Numbers: 6328
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java