Q. Write a program to accept real number x and integer n and calculate the sum of first n terms of the series x+ 3x+5x+7x+…
Program
#include<stdio.h>
int main()
{
int x,n,sum=0,i;
printf("enter value of x : ");
scanf("%d",&x);
printf("enter limit n : ");
scanf("%d",&n);
n=n*(2);
for(i=0;i<n;i++)
{
if(i%2!=0)
{
sum=sum + (i * x);
}
}
printf("sum of series : %d",sum);
}
Output:
enter value of x : 2
enter limit n : 5
sum of series : 50
Program
#include<stdio.h>
int main()
{
int x,n,sum=0,i;
printf("enter value of x : ");
scanf("%d",&x);
printf("enter limit n : ");
scanf("%d",&n);
n=n*(2);
for(i=0;i<n;i++)
{
if(i%2!=0)
{
sum=sum + (i * x);
}
}
printf("sum of series : %d",sum);
}
Output:
enter value of x : 2
enter limit n : 5
sum of series : 50
0 Comments