Hello!
I am having a problem in this program.In it a have to calculate the value of π from the infinite series
π=4-4/3+4/5-4/7+4/9-4/11+.....
by using this formula i have to print a table that shows the approximate value of π after each
of the first 1,000 terms of this series.
But the code i have written is having problems and do not gives the required results. Can any one
help? I'll be thankful

#include<iostream.h>
#include<conio.h>
void main()
    {
    float pi=4;
    double i,j,a=3,b=5;
    cout<<"The value of pi :"<<endl;
    for(i=1;i>-1;i++)
        {
    for(j=1;j>-1;j++)
        pi=pi-4/a+4/b;
        a+=4;
        b+=4;
        if(i==1000||i==i*1000)
        cout<<pi<<endl;
        }
    getch();
    }

Hello!
I am having a problem in this program.In it a have to calculate the value of π from the infinite series
π=4-4/3+4/5-4/7+4/9-4/11+.....
by using this formula i have to print a table that shows the approximate value of π after each
of the first 1,000 terms of this series.
But the code i have written is having problems and do not gives the required results. Can any one
help? I'll be thankful

#include<iostream.h>
#include<conio.h>
void main()
    {
    float pi=4;
    double i,j,a=3,b=5;
    cout<<"The value of pi :"<<endl;
    for(i=1;i>-1;i++)
        {
    for(j=1;j>-1;j++)
        pi=pi-4/a+4/b;
        a+=4;
        b+=4;
        if(i==1000||i==i*1000)
        cout<<pi<<endl;
        }
    getch();
    }

I'll leave it to others to weigh in on "void main", "conio.h", and "iostream.h". The indentation is a little off so it's hard to see what you intend to be inside what block of code:

for(i=1;i>-1;i++)
        {
    for(j=1;j>-1;j++)
        pi=pi-4/a+4/b;
        a+=4;
        b+=4;
        if(i==1000||i==i*1000)
        cout<<pi<<endl;
        }
    getch();
    }

In particular, what code is supposed to be inside the for-loop starting on line 3? Right now lines 5 - 8 are outside of that loop. Is that what you want? The indentation makes the program impossible to read.

Line 7 - i would be equal to i times 1000 if and only if i was 0 so if that's what you want, change it to:

if(i==1000||i==0)

I doubt that's what you want though.

As for lines 1 and 3, you said you wanted an INFINITE series and you have one! Or pretty close to it. These loops are going to be going on for quite some time. Check your loop condition and make sure it's what you want.

Thank you for help. I'll again try with the changes you have mentioned. Thanks alot....

Let me just add, since i is declared as a double , your loops are complete and test with i==1000 are complete junk.

If you add 1 to a double at about 1e16 this is true:

// True on some machine and about this value
double m=1e16;
if (m==m+1)
   std::cout<<"m+1 == m"<<std::endl;

Thus your inner loop will never exit.

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.