Question

In: Computer Science

write a java code Write a generic program to compute the sum of 30 terms of...

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

Solutions

Expert Solution

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));
}

}
}


Related Solutions

write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
Provide the Java code to compute the sum, average, maximum number and minimum number if I...
Provide the Java code to compute the sum, average, maximum number and minimum number if I have a string sentence of double numbers. Assume that the length of the string sentence is not known. It can be of any length. To split a string based on the comma character use the following. String sentence = "A,B,C,D,E"; String[] stringsArray = receivedSentence.split(","); Then stringsArray is an array of five elements such that: stringsArray[0] = 'A' stringsArray[1] = 'B' stringsArray[2] = 'C' stringsArray[3]...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
please write a java code, one for method and another for the Demo to: -Compute the...
please write a java code, one for method and another for the Demo to: -Compute the average age of all female students. -Compute the least amount of credits completed among males. Store the three arrays in the demo file. Print the results within the demo file. String []gender ={"F", "F", "M", "F", "F", "M", "M", "M", "M", "F", "M", "F", "M", "F", "F", "M", "M", "F", "M", "F"}; int []age = {18, 19, 19, 21, 20, 18, 24, 19, 21,...
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Write MIPs program that will read two intergers from the user and compute for the sum...
Write MIPs program that will read two intergers from the user and compute for the sum and difference of the two intergers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Write a Java program to compute the income after tax of an employee based on the...
Write a Java program to compute the income after tax of an employee based on the following rule of tax rate. Assuming the salary is $22000, display the tax and the income after tax. 12% if salary ≥ 25,000 5% if salary < 10,000 Otherwise 8% will be applied Write a Java program to compute and display the sum of the numbers that can be both divisible by 6 and 8 from 1 to 500. Suppose there is a list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT