# Incrementing fractions based on user input?

Page 1 of 1## 11 Replies - 936 Views - Last Post: 23 October 2011 - 03:22 PM

### #1

# Incrementing fractions based on user input?

Posted 21 October 2011 - 03:03 PM

I'm having a bit of a hard time figuring out how to make a program that works out pi using the infinite sequence based on user input.

e.g.

User inputs "5",

Program works out that pi = 4 - 4/3 + 4/5 - 4/7 + 4/9

User inputs "8",

Program works out that pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15

etc.

Basically I have no clue how to make the input convert to number of terms without coding all posibilities (obviously not practical). Does anyone have any pointers? I hope I'm not just being dense

Thanks

##
**Replies To:** Incrementing fractions based on user input?

### #2

## Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:10 PM

int userInput; //get a value from the user for (int i = 0; i < userInput; i++) { //do whatever you need to do here }

### #3

## Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:13 PM

I guess it is a simple for loop from 1 to limit input by user and the vakue starts at 4

depending of i % 2 the operation is + or -

the numerator is always 4

the denomitor starts at 3 and is always denominator += 2 for the next iteration

### #4

## Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:30 PM

pbl, I haven't actually started it yet, I'm just trying to wrap my head around how I'd even start this :-(

I think the main problem I'm having is the fact that the user could input 4234723482 or whatever and it would have to work out pi to that number of terms.

### #5

## Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:39 PM

### #6

## Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 03:52 PM

### #7

## Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 04:18 PM

pbl, on 21 October 2011 - 03:13 PM, said:

I guess it is a simple for loop from 1 to limit input by user and the vakue starts at 4

depending of i % 2 the operation is + or -

the numerator is always 4

the denomitor starts at 3 and is always denominator += 2 for the next iteration

I ♣ Seals, on 21 October 2011 - 03:39 PM, said:

I'm going to sleep on this and try again tomorrow, thank you both for your help, I will post again tomorrow with how I get along :-)

### #8

## Re: Incrementing fractions based on user input?

Posted 21 October 2011 - 06:08 PM

HapticThreek, on 21 October 2011 - 06:30 PM, said:

I think the main problem I'm having is the fact that the user could input 4234723482 or whatever and it would have to work out pi to that number of terms.

Still sign based on iteration index +/- based on index % 2

Still denominator += 2

### #9

## Re: Incrementing fractions based on user input?

Posted 22 October 2011 - 01:12 PM

I ♣ Seals, on 21 October 2011 - 03:39 PM, said:

pbl, on 21 October 2011 - 06:08 PM, said:

HapticThreek, on 21 October 2011 - 06:30 PM, said:

I think the main problem I'm having is the fact that the user could input 4234723482 or whatever and it would have to work out pi to that number of terms.

Still sign based on iteration index +/- based on index % 2

Still denominator += 2

OK. Here is what I have (I realise that I haven't done the main bit.

public class Pi { public static void main (String [] args) { System.out.println("Please input number of terms"); double pi = 4.0, answer; int input = KBInput.readInt(), numerat = 4, denom = 3; System.out.println("Pi will be calculated over " + input + " terms."); if (input == 0) { answer = 0; System.out.println("Pi over " + input + " terms is " + answer + "."); System.out.println("Try a number above zero please!"); } else if (input == 1) { answer = pi; System.out.println("Pi over " + input + " terms is " + answer + "."); } else { for (int i = 0; i < input; i++) { // ARRRRGHHH! } } } }

I know that if i % 2 is 0 then the next operator will be - and if its 1 then + will be the operator. I also know that the final denominator will be (input * 2 - 1). I'm guessing that +=2 means it goes up by 2 each time starting at 3, but I dont know how to code this in. I'm sorry I'm at a block here! And I feel as if I'm being a bit of a block head too! heh, appreciate your help

### #10

## Re: Incrementing fractions based on user input?

Posted 23 October 2011 - 08:52 AM

double pi = 4.0; for (int i = 1; i < input; i++) { // The following line deals with the sign double numerator = (i % 2 == 0) ? 4.0 : -4.0; // I'll leave the rest for you to complete }

This post has been edited by **I ♣ Seals**: 23 October 2011 - 08:53 AM

### #11

## Re: Incrementing fractions based on user input?

Posted 23 October 2011 - 02:01 PM

after 10000 iterations I have

3.1414926535900345

3.1416926435905346

public static void main(String[] args) { double x = 4.0; int nbIter = 10000; double denom = 3.0; double sign = -1.0; for(int i = 0; i < nbIter; ++i) { double elem = 4.0 / denom; x = x + (sign * elem); System.out.println(x); denom += 2.0; sign = sign * -1.0; } }

### #12

## Re: Incrementing fractions based on user input?

Posted 23 October 2011 - 03:22 PM