Print Sum of series 1+ (1+2) + (1+2+3)……. (1+2+3+…….n) in Java



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

Output:
Enter the no of terms 
4
Sum of series = 20
BUILD SUCCESSFUL (total time: 2 seconds)


No comments:

Post a Comment