This is a Java Program to Add the nth Square Series.
Enter the number of terms upto which you want to add the series as an input. Now we use for loop to get the desired output.
Here is the source code of the Java Program to Add the nth Square Series. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Square_Series
{
public static void main(String[] args)
{
int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.println("Given series:1^2 + 2^2 + 3^2......... +n^2");
System.out.print("Enter value of n:");
n = s.nextInt();
for(int i = 1; i <= n; i++)
{
sum = sum + i * i;
}
System.out.println("Sum of series:"+sum);
}
}
Output:
$ javac Square_Series.java $ java Square_Series Given series:1^2 + 2^2 + 3^2......... +n^2 Enter value of n:5 Sum of series:55
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Java Books
- Practice Programming MCQs
- Apply for Java Internship
- Practice Information Technology MCQs
- Check Programming Books