Follow on Facebook

Like and Share on Facebook

Wednesday, February 11, 2015

Write a Program to Sum of the Series (1)+(1+2)+(1+2+3)..N in Java



import java.io.*;
import java.lang.Math;

class nsumfact
{
public static void main (String args[]) throws IOException
{
int j, sum=0, i, n;


InputStreamReader ir=new InputStreamReader(System.in);

BufferedReader br =new BufferedReader(ir);

System.out.println("Please Enter the value of N");

n=Integer.parseInt(br.readLine());

System.out.println("");

for(i=1;i<=n;i++)
{
System.out.print("(");

for(j=1;j<=i;j++)
{
if(j==1)
{
System.out.print(+j);

}
else if(j>i)
{
System.out.print(+j);

}
else
{
System.out.print(" + "+j);

}


sum=sum+j;
}


System.out.print(")");

if(i==n)
{
System.out.print(".");

}
else
{
System.out.print("+");
}


}

System.out.print("\n\n Sum of the above Series is ="+sum);



}
}

No comments:

Post a Comment