113. JAVA Program to Compute the 1+4+9+16+ …….+n Series.
How does this program work?
- This program is used to find the Sum of the given series 1+4+9+16+ …….+n using java.
- The integer entered by the user is stored in variable n i.e., last term for the given series.
- Declare variable sum and Initially initialize with 0.
- By using for loop we can find the sum of series for square of numbers upto nth term.
Here is the code
//To Print Series of Square of numbers for nth series 1+4+9+16+ …….+n
import java.util.Scanner;public class series
{
public static void main(String[] args )
{
int n, sum = 0;
System.out.println("Series:1^2 + 2^2 + 3^2......... +n^2");
System.out.print("Enter value of n:\n");
Scanner skill = new Scanner(System.in);
n = skill.nextInt();
for ( int i = 1; i <= n; i++ )
{
sum = sum + i * i;
}
System.out.println("Sum of series:"+sum);
}
}