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

[code]...

View Replies


ADVERTISEMENT

How To Break Out Of A Loop If There Is IndexOutOfBoundException

Jan 6, 2014

Is it possible to break out of a loop if there is an indexOutOfBoundException? For example:

int v = 987;
int c = 783;
int[] someArray = new int[23];
break;
do{
//stuff
if(someArray[68] == indexOutOfBoundException){
break;
}
}while(v > c);

View Replies View Related

Write A Program That Will Call A Method To Calculate Function

Dec 3, 2014

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:

x f(x)
1 -15
2 -12
3 -7
4 0
5 9
6 20
7 33
8 48
9 65
10 84

I guess it also has to print this output in a table even though my professor never mentioned it. Usually

public class Lab12 {
public static int f(int x)
{
return x*x-16;

[code]....

View Replies View Related

Write A Program To Calculate Telephone Charges For Four Customers

Apr 13, 2014

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

************************************
java.jpg
************************************

I just need the totalX in my for loop to print the total but the total i want to print it is in the while loop

View Replies View Related

Write A Small Program That Will Calculate Gain / Loss Of Sale Of Stock

Jan 24, 2014

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();
 
[code]....

View Replies View Related

For Loop Program - Calculate Total Earnings For 30 Days

Oct 5, 2014

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

[code].....

View Replies View Related

Write A Java Program That Uses A While Loop

Apr 9, 2014

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.

View Replies View Related

Using For Loop To Calculate Power?

Oct 16, 2014

I'm new to java and i'm trying to figure out how to calculate power in java using the for loop.

The geometric sequence is (d*c^(n-1)).

So it goes d, d*c^1, d*c^2,......., d*c^(n-1)

??????for (i=1; i<=n; i++){

result

}????????

How to program this. how do i write this using for loop?

View Replies View Related

Loop To Work With Formula To Calculate New Value Of CD

Jan 28, 2014

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

[Code] ....

View Replies View Related

How To Use For Loop To Calculate Product Of Consecutive Numbers

Feb 26, 2015

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

View Replies View Related

For Loop - Calculate Total Of Series Of Numbers?

Nov 10, 2014

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

[Code] .....

View Replies View Related

Write A Method That Uses For-loop To Perform?

May 19, 2014

which when user enter a number then will display how many * in a line .Produce a line: ***** But my code only show 1 * there... what wrong with it ?

import java.util.Scanner;
public class display {
public static void main(String args[]) {
int i,n = 0 ;

[Code] ...

View Replies View Related

Write Nested Loop For Pyramid Pattern

Sep 26, 2014

Write a nested loop for a pyramid pattern. Well here's an ugly description of what it should look like:

1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 128 64 32 16 8 4 2 1

I managed to make the pyramid using addition. However, how to turn it into multiplication x 2.

import java.util.*;

public class NumbersPyramid
{
public static void main(String[] args) {
int odd = 1;
int numSpaces = 7;

[code]....

View Replies View Related

Loop That Calculate Distance Traveled - Why Java Not Printing Method Results

Apr 13, 2014

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();
 
[Code] .....

View Replies View Related

How To Use Break Points In Netbeans 8

Aug 19, 2014

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)...

View Replies View Related

Break Down Fibonacci Method

Jan 21, 2014

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.

View Replies View Related

Break Counter - Txt View Start From 0 To 30

Apr 1, 2014

I have two txt view

I have a counter that it starts from 0

I want it keep on to 30 and then stop and next txt view start from 0 to 30...

View Replies View Related

How Does Program Know To Calculate For Only One Year

Mar 26, 2015

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.

[code]....

View Replies View Related

Create A Program That Will Calculate Different Tax Brackets

Oct 3, 2014

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

[code]....

View Replies View Related

Program To Calculate Area Of Circle

Mar 11, 2015

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;

[Code] .....

View Replies View Related

Cannot Get Average Program To Calculate Properly

May 9, 2010

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.

[Code] ....

View Replies View Related

Java Program To Calculate Diameter?

May 6, 2014

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

View Replies View Related

Calculate Rainfall Quarterly Program

Mar 29, 2015

//calculates rainfall quarterly program
 
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

[Code] ....

I have everything but the min and max numbers.

View Replies View Related

Java Program Method To Calculate Distance

Feb 20, 2015

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.

View Replies View Related

Program That Calculate Change Due To Customer By Denomination

Sep 20, 2014

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).

View Replies View Related

Creating Program With Constructors And Calculate Result

Feb 11, 2015

The end result is when I print the total it comes out to be 0. Trying to solve this

package pay;
import java.util.*;
public class manager
{
public manager(String string, int i, String type) {

[Code] ......

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved