First Problem



#include <stdio.h>
#include <math.h>
#define Pi M_PI

int main(){

  int n;
  double sum, term, angle, x, y, newx;

  printf("Input a number > ");
  scanf("%lf",&angle);

  x = angle*Pi/180;  /* go from degrees to radians */

  y = floor((x + Pi) /(2*Pi)); /* Find the largest integral multiple of 2*Pi less than or equal to x + Pi */

  newx = x - y*2*Pi;   /* switch to an angle between -Pi and Pi */

  sum = 1;
  term = 1;
  n = 0;

  do{
    n = n+1;
    term = term*newx/n;    /* calculate new term */

  switch(n%4) {
  case 0:
    sum = sum + term;      /* add for 1,5,9,... */
    break;
  case 2:
    sum = sum - term;      /* subtract for 3,7,11,... */
    break;
  default:
    break;
      }
  } while(fabs(term) > 0.000001); /* Leibniz test */

  printf("The cosine of %lf is %lf.\n",angle,sum);
  printf("We needed %d steps to calculate it.\n",n-1);
  printf("The machine value cosine of %lf is %lf.\n",angle,cos(x));

    }

Second Problem


#include <stdio.h>
#include <math.h>
#define Pi M_PI

double mycos(double x){

  int n;
  double sum, term, y, newx;

  y = floor((x + Pi)/(2*Pi));
  newx = x - y*2*Pi;

  sum = 1;
  term = 1;
  n = 0;

  do{
    n = n+1;
    term = term*newx/n;

  switch(n%4) {
  case 0:
    sum = sum + term;
    break;
  case 2:
    sum = sum - term;
    break;
  default:
    break;
      }

  } while(fabs(term) > 0.000001);

  return(sum);

}

int main(){

  int i;
  double x;

  printf("Degree \t Mycos \t Cos \n");
  printf("---------------------------------\n");
  for (i = 0; i <= 360; i=i+1){
    x = i*Pi/180;
    printf("%3d \t %lf \t %lf\n",i,mycos(x),cos(x));

    }
}

Third Problem



#include <stdio.h>
#include <math.h>
#define Pi M_PI

int main(){

  int i;
  double x;
  FILE *fp;

  fp = fopen("output.txt","w");
  fprintf(fp,"Degree \t Sine \t \t Cosine \t Tangent\n");
  fprintf(fp,"--------------------------------------------------\n");

  for (i = 0; i <= 90; i = i+1){
    x = i*Pi/180;
    fprintf(fp,"%2d \t %f \t %f \t %f\n",i, sin(x), cos(x), tan(x));
}

  fclose(fp);
}