Hey guys, I need some help.
I have to find the sum of all the terms up to the nth term that I choose.
I can't figure out how to construct a while loop for it though? I am a rookie doing this so sorry if its a dumb question. Anything helps.
Thanks in advance.
Code:
#include<stdlib.h>
#include<stdio.h>
int fact;
int factorial(int n);
int power(float,int);
int Power(int,int);
int main()
{
int i, n;
float x, FS, NT, SS;
printf("The Series is 1 - 2(x^2)/1! + 3(x^4)/2! - 4(x^6)/3! +....+ [(-1)^n)](n+1)(x^2n)/n!\n");// Series Provided
printf("What is you value for n and x, respectively:\n");// Input n and x
scanf("%d %f", &n, &x);
printf("Your inputs for n and x were\nn=%d\nx=%f\n", n, x);
FS=1;// FS = First Term from Series
NT=(power(-1,n)*(n+1)*power(x,2*n))/factorial(n);// NT = Nth Term
SS=1;// SS = Series Sum
i=1;
while(i<=n)//loop
{
FS=FS+NT;
SS=SS+FS;
i=i+1;
}
printf("The result is %f\n", NT);
system("pause");
return 0;
}
int factorial(int n)// Factotial function
{
if(n==1)
{
return 1;
}
else
{
fact = n * factorial(n-1);
return fact;
}
}
int power (float x, int n)// Exponent Function
{
int i, var=x;
if (n==0) return 1;
for (i=1; i<n; i++)
x*=var;
return x;
}