How to write a program to print the sum of the following series:
s=(1+2)/(1*2)+(1+2+3)/(1*2*3)+ - - - - - - upto n terms in java using bluej.
Answers
Answer:
This is the required program for the question in Java:
import java.util.*;
public class Sum {
public static void main(String args[]) {
int n,i;
float s=0.0f,p=1.0f,q=1.0f;
Scanner sc=new Scanner(System.in);
System.out.print("Enter limit - ");
n=sc.nextInt();
sc.close();
for(i=2;i<=n+1;i++) {
q+=i;
p*=i;
s+=q/p;
}
System.out.println("Sum is: "+s);
}
}
Note: Here, p calculates the values of denominator part and q calculates the value of numerator part of each term in the series. Then, the value of q/p is added to the sum variable. The sum is displayed on the screen after calculation.
=> Input : n = 4
=> Output : 3.91667
=> Series : (1 / 1) + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + (1 + 2 + 3 + 4) / (1 * 2 * 3 * 4) = 1 / 1 + 3 / 2 + 6 / 6 + 10 / 24 = 3.91667
=> Input : n = 6
=> Output : 4.07083
=> Series : (1 / 1) + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + (1 + 2 + 3 + 4) / (1 * 2 * 3 * 4) + (1 + 2 + 3 + 4 + 5) / (1 * 2 * 3 * 4 * 5) + (1 + 2 + 3 + 4 + 5 + 6) / (1 * 2 * 3 * 4 * 5 * 6)
=> 1 / 1 + 3 / 2 + 6 / 6 + 10 / 24 + 15 / 120 + 21 / 720
=> 4.07083
I hope this will help you dear..
Always stay safe and stay healthy..