Sum of Series 1 + 4 + 9 + 16 ... n

 Logic: (1^2) + (2^2) + (3^2) + (4^2) …. n

import java.util.*;
class SumSeries_1_4_9_16
{
    public static void main(String args[])
    {
        int i, n,sum=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the terms of Series = ");
        n=sc.nextInt();
        for(i=1;i<=n;i++)
        {
            sum=sum+(i*i);
        }
        System.out.print("Sum of Series = "+sum);
    }
}

No comments

Powered by Blogger.