Hello every one i need to help me in understanding this small program
...you can approximate by using the following series
∏=4(1-1/3+1/5-1/7+1/9-1/11+1/13-.....-1/(2i-1)+1/(2i+1))
write a program that displays the value for i=10000,20000,....,and 100000
I've developed this code to solve the problem

``````public static void main(String[] args) {
int count=1;
double sum=0;
for(int i=1;i<2*i+1;i+=2)
{
if(count % 2==0)
sum-=1.0/i;
else
sum+=1.0/i;
count++;
}
double p=4*sum;
System.out.println("The value of p is "+p);
}``````

my question
is this code I've written solve the problem?
and if yes, what about "compute the value when i is equal to 10000,20000,.....,and 100000"
that is supposed to be implemented in my code

Put that code into a different method (not main) that accepts an integer as a paramter (and change i<2*i+1 to i<2*parameter+1) and then call that method from main with 10000, 20000, etc (which can be figured in a for loop from 1 to 10 multiplying this number by 10000).

## All 3 Replies

Put that code into a different method (not main) that accepts an integer as a paramter (and change i<2*i+1 to i<2*parameter+1) and then call that method from main with 10000, 20000, etc (which can be figured in a for loop from 1 to 10 multiplying this number by 10000).

Thanks very much but i take another way by using nested loops
and this gave me true result....this is the code

``````public static void main(String[] args) {
int count=1;
double sum=0;
double p;
for(int i=10000;i<=100000;i+=10000)
{
for(int x=1;x<=2*i+1;x+=2)
{
if(count % 2==0)
sum-=1.0/x;
else
sum+=1.0/x;
count++;
}
p=4*sum;
System.out.println("The value of p is "+p);
sum=0;
count=1;
}
}
}``````

I'd be grateful if you give me any comments about my code to improve my writing skills
thanks......

To each his own. You need to start using methods and the such though, and this was a good opportunity to try it out.

Be a part of the DaniWeb community

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