Print Series 2, -4, 6, -8,………n terms in Java



import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  int c, i = 2, n;                                          // c for counter, i for even nos.
  System.out.print("Enter the number of terms: ");
  n = sc.nextInt();
  System.out.print("\n");
  for (c = 1; c <= n; c++, i += 2)               //to generate n terms of the series
  {
   if (i % 4 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
  }
 }
}

Output:

Enter the number of terms: 10
2 -4 6 -8 10 -12 14 -16 18 -20 
BUILD SUCCESSFUL (total time: 6 seconds)



1 comment: