NameSilo

Java help please(simple question for most java users)

Spaceship Spaceship
Watch

counter

Account Closed
Impact
1
java help please(simple question for most java users)
hey guys
i am just new to java and i got a java project i need help with please can someone help me

what i am asked to do is this
Calculate the value of pi from the infinite series
π= 4 - 4/3 + 4/5 - 4/7 + 4/9 +...
Print a table that shows the value of pi approximated by computing one term of this series, by two terms, by three terms, and so on. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159?


Any suggestions (or how to start it )
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
please anyone can help me
 
0
•••
I'm not quite sure how I would do it in Java, as I don't know Java. Nor do I want to do your homework for you, so i'll post some python code that would do it. You can try and translate it into Java.

Code:
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-

terms = 1 # This is what the terms start as.
pi = "4" # This is the equation to calculate Pi
maxTerms = 5000 # This is the maximum number of terms to have.

while terms <= maxTerms:
     if terms % 2 == 0: # If the number of terms is even
          pi += " + 4.0/" + str((3 + 2 * (terms - 1))) # Calculate the fraction to add.
     else: # If the number of terms is not even, ie; odd
          pi += " - 4.0/" + str((3 + 2 * (terms - 1))) # Calculate the fraction to subtract.

     newpi = eval(pi) # Calculate the new value of pi using the equation
	
     print "Terms", terms, "- Pi", newpi # Print the results

     terms += 1 # Increment the terms

That code will generate maxTerms lines of output, of the format "Terms x - Pi y"
 
Last edited:
0
•••
tried to convert but

i tried to convert it i got the result below it giving me lots of number i dont know what they r they r not close to to pi value 3.14
Code:
public class Problem3
{
	public static void main(String arg[])
	{
		
			double terms = 1;//This is what the terms start as.
			double pi = 4; //This is the equation to calculate Pi
			double maxTerms = 1000;//This is the maximum number of terms to have.
			while(terms<=maxTerms)
			{
				if(terms%2==0)//If the number of terms is even
				{
					pi += (4.0/ (3 + 2 * (terms - 1))) ;//Calculate the fraction to add.
				}
				else
				{
					 pi += ( - 4.0/(3 + 2 * (terms - 1))) ;//Calculate the fraction to subtract.
				}
				double newpi = pi;// Calculate the new value of pi using the equation
				double mac = terms-newpi;
				System.out.printf("Terms %s",mac);// Print the results
				terms += 1 ;//Increment the terms
			}
	
	}
}


anyways
what is newpi = eval(pi) # Calculate the new value of pi using the equation
what does it do is it just set new pi to the new value

anyways ur code what did u aim it to do

Please guys help me i still got less than 3 hours till submittion deadline and i did all problems (even more complex than this) but i got no clue how to do this


finally i tried to do this code thats the closest i could get to but am sure its wrong



Code:
import java.util.Scanner; // import scanner
public class Problem3
{
	public static void main(String arg[])
	{
		
			
			double total2 = ((4/3)+(4/5));// setups the sequence
			double final1 =4-total2; //initiales it 
			double total =0;//initialiez the total
		for(int i=3; i<30;i=i+4 )//for loop
		{
			double a = 4;//
			double b = i+2;//incremation value
			double c=a/b;
			
			total =  a-c;	//subtracot		
			System.out.printf(" %s \n",total);// display result 
		
		}
	}
}


please also if any knows whats wrong (or how to make a correct one )
please let me know asap

thx for ur support in advance
 
0
•••
counter said:
anyways
what is newpi = eval(pi) # Calculate the new value of pi using the equation
what does it do is it just set new pi to the new value
pi contains the equation in string form (ie: "4 - 4/3 + 4/5 - 4/7 + 4/9 +...") newpi = eval(pi) runs pi as if it was a line of code, and assigns the output to newpi.

counter said:
anyways ur code what did u aim it to do
Calculate pi up to maxTerms terms in the series. It works, because i'm running it now.
 
0
•••

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back