Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

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

Output:

Enter the no of terms 
5
Sum of the series : 0.904412037037037
BUILD SUCCESSFUL (total time: 3 seconds)
import java.util.Scanner;
public class OddSeries 
{
 public static void main(String args[]) 
  {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int i = 1, c, f = 1;                            // i for odd nos, c for counter, f for flag
  for (c = 1; c <= n; c++) {
   if (f % 2 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
   i += 2;
   f++;
  }                                                  //Loop ends
 }
}


Output:


Enter the number of terms: 6

1  -3  5  -7  9  -11...... 
BUILD SUCCESSFUL (total time: 6 seconds)

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)