Yêu cầu:
Lập chương trình tính sin(x) với độ chính xác 0.0001 theo công thức:
sin(x) = x – x^3/3! + x^5/5! – …+(-1)^n.x^(2n+1)/(2n+1)!
Thuật toán:
Code:
/************************************************************ * Author: VNCODING * History * 2014/03/18 first create VNCODING *************************************************************/ #include "stdio.h" #include "conio.h" #include "math.h" #define E 0.0001 #define PI 3.141592654 long giaithua(int N); void main() { float angle; // goc float e = 1; // sai so int n = 0; float sinx = 0; printf("\n Nhap vao so do goc (do) = "); scanf("%f", &angle); while(e > E) { e = pow(float(angle*PI/180), (2*n+1))/giaithua(2*n+1); // tinh sai so if(n%2 == 0)// n chan { sinx = sinx + e; } else // n le { sinx = sinx - e; } n++; } printf("\n sin(%f) = %f", angle, sinx); getch(); } long giaithua(int N) { if(N == 0 || N == 1) return 1; else return N*giaithua(N-1); }
Kết quả:
Nhap vao so do goc (do) = 45 sin(45.000000) = 0.707106
Leave a Reply
You must be logged in to post a comment.