In: Computer Science
write a java code
Write a generic program to compute the sum of 30 terms of the following series.
(181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + .
. . . .
1 5 11
Note: the 3rd, 6th, 9th term etc, has a negative sign in front of
parenthesis.
User Input:
None
Expected output:
Term Ratio Sum
1 5.677 5.677
2 5.735 11.413
3 -4.811 6.602
import java.lang.Math;
public class Main {
public static void main(String[] args) {
float Sum = 0;
float a = 181;
float b = 5;
float c = 31;
float Ratio = a;
System.out.println("Term\t\tRatio\t\tSum\t\t");
for(int i = 1; i <= 30; i++)
{
if(i == 1) Ratio = (float)(a - b) / c;
else{
a = a + 5;
b = (float)Math.pow(-1,i)*(b+4);
c = c + 3;
Ratio = (float)Math.pow(-1,i)*(((a + b)) / c);
}
Sum = Sum + Ratio;
System.out.println(i+"\t\t"+String.format("%.4g",Ratio)+"\t\t"+String.format("%.4g",Sum));
}
}
}