Write A Program Using Loop And Break To Calculate Value Of Pi
Oct 5, 2014
having some trouble doing this. Here are the requirements:
1)Write a program using loop and break to calculate the value of PI.
2)You can use the following equation to calculate PI. PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13 etc.. The program shouldn't loop until the criteria is satisfied.
3)The criteria is the value of PI you get is very close to the “real” value (3.1415926), meaning that their difference
should be smaller than a very small numeric number, e.g., 0.0001).
4)The control structures you may need for this program are loop, if-else, and unlabeled break.
5)The output should have the format: for each iteration, it has iteration number and the corresponding value of PI until that
iteration. The last line would be the final value of PI.
Here is my code so far:
package ids;
public class IDS {
public static void main(String[] args) {
double pi = 0;
for(int i=1; i<=100000; i++){
if ((i%2)==0){
pi+=(4/(2*i-1));
I have understood my programming class up to this point and now I have been given a lab that I can't figure out for the life of me. Here what I have to do: Write a program that will call a method (called f) to calculate the following function" f(x)=(x^2)-16...this is what the output should be:
Write a program to calculate the telephone charges for four customers. The data for each customer consists of NAME, PREVIOUS METER READING, PRESENT METER READING. The difference between the two readings gives the number of units used by the customer for the period under consideration.
The amount due for each customer is calculated by: UNITS USED * RATE PER UNIT + RENTAL CHARGE The rate per unit and rental charge is assumed to be the same for all customers and must be input once only.
Your program must:
a. Input the rate and rental charge b. Read the data for each customer and calculate the amount due c. Print the information under suitable headings d. Calculate and print the total amount due to the telephone company
I am trying to write a small program that will calculate the gain and/or loss of the sale of stock. The program will ask the user for the number of shares, the purchase price and the selling price. I am pretty sure that the errors is coming from my calculations in the program.
import java.util.Scanner; public class investmentCalculator { public static void main (String[] args) { Scanner input = new Scanner(System.in); //User input for number of shares System.out.print("Enter the number of shares: "); double shares = input.nextDouble();
So I've attempted a for loop for this type of problem:If you receive $0.01 on the first day of your allowance, and the daily rate doubles each day after, calculate the total earnings for 30 days.
Output should be like: Day ----- Daily allowance - Total allowance 1 -------- 0.01 -------------- 0.01 2 -------- 0.02 -------------- 0.03 3 -------- 0.04 -------------- 0.07
My code so far:
public class Allowance { public static void main (String[] args) { int day = 0; double daily = 0.0; double total = 0.1; System.out.println("Day Daily Allowance Total Allowance"); for(day = 0; day <= 30; )
Write a java program that uses a While loop and persistent output to count down from 33 to 0 by threes, and put each value on a separate line, also do this in for loop.
It's suppose to generate a table with the month, and the new amount of the CD. Right now, the program generates a continuous table for months, but it doesn't update the value of the CD.
import java.util.*; public class Excercise04_31 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in);
I wanted to know if I was off to the right start. I am trying to write a program using the for loop the calculate the product of the consecutive numbers 4 through 8 but so for I am getting 3 values output and I only want 1 value at the print out.
The code I am using outputs the numbers too large. I am trying to see where I went wrong.
for ( int i = 4 ; i <= 8; i++) { int j = i++; int k = j++; int l = k++; int m = l++; System.out.println( + (i*j*k*l*m) ); }
So basically, I've been trying to create a for loop that asks the user for a number between 1 and 30, then calculates the total of the series of numbers. For example, if 15 is entered, the calculation should be 1/15+2/14+3/13+...15/1 which would equal approximately 38.1. The problem I'm having is that the code doesn't loop whenever I type a number for some reason, which results in a very incorrect calculation. Here is my code so far:
import java.util.Scanner; public class HmwLoop { public static void main(String[] args) { double sum = 0; for (double num1 = 1, num2 = 30; num1 <= 30 && num2 >= 1; num1++, num2--)
I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter Hours Traveled "); int hoursTraveled = input.nextInt(); System.out.println("Enter MPH "); int mph = input.nextInt();
I want to know that how can i use break points in net beans 8. I'm able to put break point by clicking at the line but don't know how to use it..Actually i'm getting wrong output of my code and i want to know my line by line execution of the code so i can get better idea of where is my code going from one line to other. I have used break point in micro soft visual studio2010 and that was easy after puting break point run the project then f11 to get line by line execution...I want to know any method exist in java net beans (i'm doing GUI swing controls)...
public class E09_Fibonacci { static int fib(int n) { if (n <= 2) return 1; return fib(n-1) + fib(n-2); } public static void main(String[] args) { // Get the max value from the command line: int n = Integer.parseInt(args[0]); if(n < 0) { System.out.println("Cannot use negative numbers"); return; } for(int i = 1; i <= n; i++) System.out.print(fib(i) + ", "); } }
please break down fib method shown above. I can't understand how the magic is happening inside that recursion.
how does the program know to calculate for only one year?
public class Interest2 { public static void main(String[] args) { double principal; // The value of the investment. double rate; // The annual interest rate. double interest; // The interest earned during the year.
I have spent the past 12 hours on it and have gotten no where. I need to create a program that will calculate different tax brackets. I have tried all combinations I can think of and can't get it to work!!! I am literally about to go insane! I need the program to calculate tax will be 1% for anything up to $50000. If the tax is over $50000, you keep that 1% of $50000 and then add the remaining. So for an income in the second range, it would be 1% of $50000, and then 2% of (income - $50000). This then continues for each range.
The problem I'm having is getting it to display the correct interest rates. I am not asking for you to write it for me. I want to learn. But at this point I have exhausted all resources available to me.
import java.util.Scanner; public class Project3taxinfo { public static void main(String[] args) { final double RATE1 = 0.01; //1% tax on the first $50,000 final double RATE2 = 0.02; //2% tax on the amount over $50,000 up to $75,000 final double RATE3 = 0.03; //3% tax on the amount over $75,000 up to $100,000 final double RATE4 = 0.04; //4% tax on the amount over $100,000 up to $250,000 final double RATE5 = 0.05; //5% tax on the amount over $250,000 up to $500,000 final double RATE6 = 0.06; //6% tax on the amount over $500,000
I'm starting with my version of very basic program: calculating area of circle. And of course it doesn't get well. My question: what is wrong in this code?
public class circleAre{ double radious; void putData(double radi){ radi = radious;
I am having a problem with my program. I can't get my program to calculate properly. Everything compiles and run but its just giving me a wrong answer. I am suposse to get 115.50 but instead I am getting .30...
order.java public class Order { double salesTaxRate; //initializing a variable for the sales tax rate. double subTotal; //initializing a varliable for the sub total. double shippingCost; //initializing a variable for shipping cost. double salesTax; //initializing a variable for sales tax. double totalCost; // initializing a variable for totale cost.
Assume the tanker is a cylinder of length 19.35 feet and volume 1320 gallons. What is the diameter in feet of the tanker truck? To solve this problem, write a Java program to calculate the diameter. For full credit, your Java application must follow these specifications.
1. The program must interactively input the capacity of the tanker in gallons and its length in feet. This should be done in the main method of your Tester class.
2. The input data should then be used to construct a TankerTruck object. You will need to write the TankerTruck class.
3. A TankerTruck method should be used by main to calculate the resulting diameter of the truck in feet.
4. Your program should use the standard Java constant for the math constant PI.
5. The output should be displayed using fixed-point notation, rounding to one decimal place of precision
import java.util.Scanner; public class SophiaKeyProgram1 { public static void main(String[] args) { // TODO Auto-generated method stub int numQuarter; //number of quarters in a year double rainfall=0; //the amount of rain
Write method distance, which calculates the distance between two points (x1, y1) and (x2, y2). All numbers and returned values should be of type double. Incorporate this method into an program that enable the user to enter the coordinates of the points, then calculate and display the distance by calling the method –distance.
I've tried numerous times to make it work and I'm on the right path, however I'm missing some things in the code to make my results look like this later on, which I've attached onto this post.
write a program that calculates the change due to a customer by denomination; that is, how many pennies, nickels, dimes, etc. are needed in change. The input is to be purchase price and the size of the bill tendered by the customer($100, $50, $20, $10, $5, $1).