home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / PI-5WAYS.ZIP / PI1.C < prev    next >
C/C++ Source or Header  |  1991-06-15  |  1KB  |  42 lines

  1. /**************************************************************************/
  2. /*               Calculate PI  by John Wallis approximation               */
  3. /*             π = 2( 2/1 * 2/3 * 4/3 * 4/5 * 6/5 * 6/7 .....)            */
  4. /*                                                                        */
  5. /*                              Mendel Cooper                             */
  6. /*                             3138 Foster Ave.                           */
  7. /*                           Baltimore, MD 21224                          */
  8. /*                                                                        */
  9. /*                                   06/91                                */
  10. /*                   Source code placed in the public domain              */                 
  11. /**************************************************************************/
  12.  
  13.  
  14. /*may need #include <math.h> */
  15.  
  16. #define MAX 5000
  17.  
  18. main()
  19. {
  20. int numerator[MAX+1], denominator[MAX+1];
  21. register int k;
  22. double Pi = 2.0; /*really!*/
  23.  
  24. for (k = 0; k <= MAX; k+=2)      /*initialize arrays*/
  25. {   numerator[k] = k + 2;
  26.     denominator[k] = k + 1;   }
  27.  
  28. for (k = 1; k <= MAX; k+=2)
  29. {   numerator[k] = k + 1;
  30.     denominator[k] = k + 2;   }
  31.  
  32.  
  33. for (k = 0; k <= MAX; k++)
  34.    {  Pi *= (double)numerator[k]/(double)denominator[k];
  35.       printf("Term #%5d ---------->  π ≈ %f  \n",k,Pi);  }
  36.  
  37. }
  38.  
  39.  
  40.  
  41.  
  42.