Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Pi calculating help.

Hi im extremly new to programming, and this is a school project i need help with.
I'm supposed to take an input from the user and calculate the value of pi using this formula: 1/1 - 1/3 + 1/5 - 1/7 etc... so on alternating between minus and plus and denominator increasing by 2 every time. If the user inputs the number 3, then it should print (1/1 - 1/3 + 1/5 - 1/7 + 1/9) *4. The idea here is that the higher the input of the user is, the higher the equation will be close to pi.

this is what i have so far, i know its pitififul /

import java.util.Scanner;

public class Pi
{
public static void main(String[] args)
{
double first = 1/1;
double second = 1/3;

Scanner input = new Scanner(System.in);
System.out.println("Enter a Number to calculate how far you want the formula to perform: ");
double count = input.nextDouble();

}
}

this is literally all i have, i know i need a while loop and an if loop. any help?

2. ## Re: Pi calculating help.

i know i need a while loop and an if loop. any help?
Do you know how to write a while loop? An if statement? See
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

anyone help?

4. ## Re: Pi calculating help.

Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

Once you've been given advice, it's helpful to know if it was useful, if you explored it, tried to apply it, had no luck with it, etc. Just throwing more advice at a poster when it's not clear they're in receive mode is a waste of time. Did you have any luck creating a loop to add a series of numbers?

5. ## The Following User Says Thank You to GregBrannon For This Useful Post:

copeg (September 6th, 2014)

6. ## Re: Pi calculating help.

i know the basics of it but i was hoping someone would walk me through the steps.

--- Update ---

```
import java.util.Scanner;

public class Pi
{
public static void main(String[] args)
{
double first = 1/1;
double second = 1/3;

Scanner input = new Scanner(System.in);
System.out.println("Enter a Number to calculate how far you want the formula to perform: ");
double count = input.nextDouble();

}
}```

--- Update ---

i tried while loop but im COMPLETLY lost

7. ## Re: Pi calculating help.

What pattern can you see in the series:

1/1 - 1/3 + 1/5 - 1/7

?

Once you've seen the pattern, write an algorithm that faithfully repeats the pattern to produce a result. How can a loop with a loop control variable be written to produce the pattern or series above the desired number of times? Find the pattern and try to write the loop. Post that - just the loop - when you have something to show you want us to evaluate and help you with.

8. ## Re: Pi calculating help.

the denominator is increasing by 2, and its alternating between - and +. so i would need to multiply -1 to every number. But i dont know how to implement this as im this is like my 6th day of programming.

9. ## Re: Pi calculating help.

Good pattern recognition. Can you write a for loop? Any for loop? You have to transfer knowledge of the basics to application of the basics to move forward.

10. ## Re: Pi calculating help.

Use a variable to multiply by that has alternating values: +1 and -1.

11. ## Re: Pi calculating help.

i dont know what a for loop does...

12. ## Re: Pi calculating help.

Take a look at the tutorial:
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)