c - Code::Blocks 13.12 error - program has stopped working -
i using codeblocks 13.12. have made program following question:-
evaluate function 0.0001% accuracy. sinx = x - (x^3/3!) + (x^5/5!) - .....
it gives error after enter value of x. says "accuracyofsinx.exe has stopped working".
#include<stdio.h> #include<conio.h> #define accuracy 0.000001 int main() { int n, num, mulx, fac, rem; float x,term, sum; printf("enter x = "); scanf("%f", &x); num=1; sum=0; mulx=1; for(n=1, term=1; term>accuracy ; n++) { fac=num; { num=num-1; fac=fac*num; /*finds factorial*/ mulx=mulx*x; /*computes x raised num*/ } while(fac>0); term = 1/fac; num=num+2; rem=n%2; if(rem==0) /* -ve sign given position*/ sum = sum-(mulx/fac); if(rem!=0) /* +ve sign given odd position*/ sum = sum+(mulx/fac); mulx = x; } printf("sin(x)=%f", sum); }
the main problem lies here:
term = 1/fac;
it case of division zero. printed value of fac
before statement, , prints 0
before accuracy.exe stops working.
the main reason fac
going 0
num=num-1; fac=fac*num;
to prevent this, add if
condition check value of num
before using calculating fac
.
Comments
Post a Comment