Understanding small program

I've written the code but I've small problem

Page 1 of 1

1 Replies - 633 Views - Last Post: 18 October 2010 - 09:27 PM Rate Topic: -----

#1 Moustafa taha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 10

Understanding small program

Posted 18 October 2010 - 04:10 PM

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);
    }


but i don't know how can i compute the value when i is equal to 10000,20000,.....,and 100000
note that the output of my program is
The value of p is 3.1415926517266946

Is This A Good Question/Topic? 0
  • +

Replies To: Understanding small program

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Understanding small program

Posted 18 October 2010 - 09:27 PM

try using BigDecimal to give you a little more memory for those hefty numbers =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1