InterviewSolution
Saved Bookmarks
| 1. |
Java program to print the output of following series 1-2+3-4+5-6.......n |
| Answer» PROGRAM to find sum of // first n TERMS of the given series import java.util.*; class GFG { static int calculateSum(int n) { // when n is odd if (n % 2 == 1) return (n + 1) / 2; // when n is not odd return -n / 2; } // Driver code public static VOID main(STRING ar[]) { // no. of terms to find the sum int n = 8; System.out.println(calculateSum(n)); } } | |