
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Nth Square Series in Java
The sum of the nth squares series is a form of arithmetic progression where the sum of squares is arranged in a sequence with the initial term as 1, and n being the total number of terms.
12 + 22 + 32 + ... + n2
In this article, we will discuss a Java program to add the nth square series. But, before that, let's see an example scenario:
Example Scenario:
Input: num = 6 Output: sum = 91
It will be calculated as: 12 + 22 + 32 + 42 + 52 + 62 = 1 + 4 + 9 + 16 + 25 + 36 = 91
Adding the nth Square Series using a for loop
The for loop will iterate till the specified nth position and will calculate the sum of the squares of each number in that range. To find the square of a number, we use the multiplication operator (*), and for addition, the plus operator (+).
Where,
-
initial expression: executed once when the loop begins.
-
Conditional expression: The code will be executed till the conditional expression is true.
-
increment/decrement expression: to increment/decrement the loop variable.
Example
The following Java program shows how to find the sum of the nth square series using a for loop.
import java.util.*; public class Main { public static void main(String[] args) { int addition = 0; int num = 6; for(int i = 1; i <= num; i++) { addition += i * i; } System.out.println("Sum of nth series till " + num + "th terms = " + addition); } }
When you run the above program, it will print the sum after completing all iterations.
Sum of nth series till 6th terms = 91
Adding the nth Square Series using a for loop
Instead of a for loop, we can also use a while loop to calculate the sum of the nth square series. Let's see its syntax first:
Example
In the following Java program, we use a while loop to find the sum of the nth square series.
import java.util.*; public class Main { public static void main(String[] args) { int addition = 0; int num = 6; int i=0; // loop is running till 6 while (i <= num) { // to perform sum of squares addition += i * i; // incrementing loop variable i++; } System.out.println("Sum of nth series till " + num + "th terms = " + addition); } }
Output of the above code is as:
Sum of nth series till 6th terms = 91
Using the Formula of the Sum of Squares
All the counting numbers are called natural numbers. Its range is from 1 to infinity. If we want to find the sum of squares of n consecutive natural numbers, then we can use the formula given below in our program:
12 + 22 + 32 + ... + num2 = ( num * ( num + 1 ) * ( 2 * num + 1 ) ) / 6
Example
Let's see how to use the above formula in a Java program to find the sum of squares:
import java.util.*; public class Main { public static void main(String[] args) { int num = 5; int addition = 0; addition = (num * (num + 1) * (2 * num + 1)) / 6; // Using the above Formula System.out.println("Sum of nth series till " + num + "th terms = " + addition); } }
When you execute the above, it will print the following result:
Sum of nth series till 5th terms = 55