Write a java program to print the sum of the following series based on
user's choice:
S 1/12 + 1/2^2 + 1/3^2.. 1/n^2
S 1/1 1/2 + 1/3 1/4...1/n
pls answer fast
Answers
Answer:
The required Java program to print the sum of series S = 1/1² + 1/2² + 1/3² + ... + 1/n² is :-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter the value of n : ");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
double sum = 0.0;
for(int i=1 ; i<=n ; i++) {
sum += (1/Math.pow(i,2));
}
System.out.println("The sum of series upto "+n+" terms is "+sum);
}
}
The required Java program to print the sum of series S = 1/1 + 1/2 + 1/3 + ... + 1/n is :-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter the value of n : ");
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
double sum = 0.0;
for(int i=1 ; i<=n ; i++) {
sum += (1.0/i);
}
System.out.println("The sum of series upto "+n+" terms is "+sum);
}
}