Find the Sum of N Natural Numbers in Java
Find the Sum of N Natural Numbers in Java
Given an integer input “num” the objective is to sum up all the numbers that lay in the interval [0,num]. To do so we’ll write a code to Find the Sum of N Natural Numbers in Java.
Example Input : 6 Output : 21 Explanation : 0 + 1 + 2 + 3 + 4 + 5 + 6 = 21.
Find the Sum of N Natural Numbers in Java
Given an integer num input as the upper limit, the objective is to sum up all the numbers that lay in the interval starting from 0 to the integer input “num”. Here are some of the methods to sum up all the numbers in a given interval
- Method 1: Using for Loop
- Method 2: Using Formula
- Method 3: Using Recursion
We’ll discuss the above mentioned methods in detail in the sections below.
Method 1: Using for Loop
In this method we’ll use for loop to iterate through the numbers in a range of [0,num].
Working
For an integer input “num” we do the following
- initialize the required variables within the class.
- Run a for loop from 0 to num+1 meanwhile appending all the numbers to sum variable.
- Print the sum variable.
Let’s implement the logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { int n = 10; int sum = 0; for (int i = 1; i <= n; i++) sum += i; System.out.println (sum); } }
Output
55
Method 2: Using the Formula
In this method we’ll use a formula to find the sum of N integers in a series from sequence and series. The formula is mentioned below.
Working
For an integer input we do the following operations
- Initialize the following variables.
- Implement the formula as sum = num * ( num + 1 ) / 2.
- Print the sum variable.
Let’s implement the above logic in Java language.
Java Code
public class Main { public static void main (String[]args) { int n = 10; System.out.println ( n*(n+1)/2); } }
Output
55
Method 3: Using Recursion
In this method we’ll use the concept of recursion to find the sum of all the number that lay from 0 to num. To know more about recursion, check out Recursion in Java
Working
For a given integer input as “num” we perform the following steps
- Initialize the required variables.
- Define a recursive function with base case as num == 0.
- Set recursive step call as num + recursum(num-1).
- Call the recursive function and print the value returned by the function.
Let’s implement the above logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { int n = 10; int sum = getSum (n); System.out.println (sum); } static int getSum (int n) { if (n == 0) return n; return n + getSum (n - 1); } }
Output
55
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Getting Started
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
public class Main
{
public static void main(String[] args)
{
//Sum of First N Natural numbers:
add(15);
}
static void add(int n)
{ //now we initial that starting point is 1 and ending point is n
//so the condition is (i!=n)
//ours work is sum of N natural number
int i=1;
int sum=0;
while (i!=n+1)
{
sum=sum+i;
i++;
}
System.out.println(sum);
}
}