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();
double sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + (double) 1 / Math.pow(i, 2);
System.out.println("Sum of series = " + sum);
}
}
Output:
Enter the no of terms
5
Sum of series = 1.4636111111111112
BUILD SUCCESSFUL (total time: 2 seconds)
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)
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)