The sum of the following series | User given limit | 1-2^2/2!+3^3/3!- . . . . . . . . | C-Program
The sum of the following series: 1 - 2^2 / 2! + 3^3 / 3! - . . . . . . .
Code:-
#include<stdio.h>
#include<math.h> // for pow( );
int fact(int a)
{ int ans=1;
for(int i=1;i<=a;i++)
{ ans *= i; }
return ans;
}
void main()
{ int i,x;
float sum=1;
int fact(int a);
printf("Enter the number :- ");
scanf("%d",&x);
printf("\n1");
for(i=2;i<=x;i++)
{
if(i%2==0)
{ printf(" - %d^%d/%d!",i,i,i);}
else
{ printf(" + %d^%d/%d!",i,i,i); }
}
for(int j=2;j<=x;j++)
{ if(j%2==0)
{ sum-=pow(j,j)/fact(j);}
else
{ sum+=pow(j,j)/fact(j); }
}
printf(" = %f\n",sum);
}
Output:
Hope you enjoyed the program, comment down blow how much you liked it.
If you have any doubt feel free to ask me in the comment.
Thank you, have a nice day.
If you have any doubt feel free to ask me in the comment.
Thank you, have a nice day.
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)