Q. Write a program to accept real number x and integer n and calculate the sum of first n terms of the series 1/x + 2/x^2 + 3/x^3 + …
Program
#include<stdio.h>
int main()
{
int x,n,i,j;
float sum=0,p,m;
printf("enter value of x : ");
scanf("%d",&x);
printf("enter limit n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
m=x;
for(j=1;j<i;j++)
{
m=(m*x);
}
p=i/m;
sum=sum+p;
}
printf("\nsum of series : %f",sum);
}
Output:
enter value of x : 2
enter limit n : 4
sum of series : 1.625000
Program
#include<stdio.h>
int main()
{
int x,n,i,j;
float sum=0,p,m;
printf("enter value of x : ");
scanf("%d",&x);
printf("enter limit n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
m=x;
for(j=1;j<i;j++)
{
m=(m*x);
}
p=i/m;
sum=sum+p;
}
printf("\nsum of series : %f",sum);
}
Output:
enter value of x : 2
enter limit n : 4
sum of series : 1.625000
0 Comments