Tuesday, 26 January 2016

Print the following series (1+2)+(1+2+3)+(1+2+3+4)………………….for n terms.

Code:

import java.util.*;
class Series
{
public static void main(String args[ ])
{
int limit, s = 0, i ,j , sum = 0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Limit: " );
limit=sc.nextInt();
System.out.println("Series:::");
for(i = 1; i <= limit; i++)
{
  for(j = 1; j <= (i+1); j++)
  {
    s = s + j;
  }
sum = sum + s;
}
System.out.println("Result = "+sum);
}
}

Output:

Enter The Limit: 
10
Series:::
Result = 990


No comments:

Post a Comment