I am trying to write a program which will solve the following expression:
(1/2)-(1/2^2!)+(1/2^3!)-(1/2^4!)+(1/2^5!).......+-(1/2^(n-1)!)+-(1/2^n!)
I have been trying to debug it for a couple of hours now and I can't understand what is wrong. I have tested it with n=5 which should return ~0.26562494 and it is returing 136.
Here's what I have:
If you happen to find outwhat is wrong, please do share it. If you notice something that could be done in a better way but doesn't have to do with this problem in particular, please share it aswell.Code:#include <stdio.h> long int factor(int num1){ int x; long int fact; fact = 1; for ( x=1; x <= num1; x++ ){ fact = fact*x; } return (fact); } float main(){ int pt, sign, n; float frac, som; pt = 1; sign = -1; som = 0; printf("Insert your n: \n"); scanf("%d", &n); for (pt = 1; pt <= n; pt++){ sign = sign * -1; frac = sign * ( 1 / ( 2 ^ factor(pt) ) ); som = som + frac; } return(som); }
Thank you very much for your patience and helpfulness
Tiago Figueiró