Sum in C cannot solve -
ok,so wrote program , compiled it. it's ok, when run in windows 7 get's me error c0000005. have no idea why.
overview of program
i want program give me sum of numbers depends of given "n" : 1-1x3+1x3x5-...+-1x3x5x...x(2*n-1).
please me,i begginer.
#include <stdio.h> #include <conio.h> int main() { int n, sum=0, i, signum; printf("give n: "); scanf("%d", n); for(i=1, signum=1; i-(2*n-1); sum+=(signum*(sum*i)),i+2,signum=-signum) ; printf("sum is: %d", sum); getch(); return 0; }
you write code this, think lot clearer.
#include <stdio.h> int main() { int i, n, sum = 1, term = 1; printf("give n: "); scanf("%d", &n); // address of n (i = 2; <= n; ++i) { // simple loop term *= -(2 * - 1); // multiply previous , flip sign sum += term; } printf("sum is: %d", sum); return 0; }
i'm on linux, removed header <conio.h>
non-standard.
Comments
Post a Comment