write java programme, using DO and WHILE to approximate 22/7 (pi)= 4-4/3+4/5-4/7+4/9-4/11+4/13..

Recommended Answers

All 4 Replies

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

I'm a first year Java student and
I need some help with a practice problem

Write java programme, using DO and WHILE to approximate 22/7 (pi)= 4-4/3+4/5-4/7+4/9-4/11+4/13..

Here is what i've tried so far:

 public static void main(String[] args){
    double sum = 0;        
    int count = 1;
        do {
            double numToAdd = 1.0 / ((i * 2) - 1);
        sum += numToAdd;
            count++;
        } while (sum += numToAdd;);
    }
}

Thank You

you have put double variable "numToAdd" inside the do-while loop and
you are accessing it in while condition like this

} while (sum += numToAdd;);// this will not compile 

This is incorrect , since variable "numToAdd" is not visible outside the flower brackets {} of do-while loop.
Hence declare the variable "numToAdd" outside the do-while loop.

Also while condition checks for boolen result

while(sum += numToAdd) // this will not compile , hence modify the condition.

while condition checks for a boolean result. Hence this code will not compile.

I also observed
(pi)= 4-4/3+4/5-4/7+4/9-4/11+4/13..

But in your code you are only doing addition , where are you doing subtraction.

double numToAdd = 1.0 / ((i * 2) - 1);//this will not compile since variable i is not declared in your code
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.