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

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,
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 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] ?".
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...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT