Program to find the sum of the series, 3+5+9+15+……..

import java.io.*;
class test
{
public static void main(String[] asd)throws IOException
{
BufferedReader obj= new BufferedReader(new InputStreamReader(System.in));
String num;
int n,sum=3, result=0;
System.out.print("Enter the value till you want the sum of the series.");
num=obj.readLine();
n=Integer.parseInt(num);
System.out.println("SERIES IS::");
System.out.print("3 ");
for(int i=0;i<n;i++)
{
sum=sum+ (2*(i+1));
result+=sum;
System.out.print(sum+" ");
}
System.out.println("Sum of the series:: "+(result+3)+".");
}
}

Leave a comment