Evaluate the following expression using for loop S= x + 2x2 +3x3+....+nxn
#include < stdio.h>
#include < stdio.h>
void main()
{
int i,sum=0,x,n,a=0;
clrscr();
printf("Enter the value of 'x' and 'n':-");
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
{
a++;
if(i < n)
printf("%d*%d^%d+",a,x,i);
else
printf("%d*%d^%d=",a,x,i);
sum=sum+a*pow(x,i);
}
printf("\t%d",sum);
getch();
}
Another program on for loop. Evaluate the expression sin x= x-x3/3!+x5/5!-x7/7!+....
#include < stdio.h>
void main()
{
float x,t,sum,d1,d2;
int i,j,n,c,counter=0;
clrscr();
printf("Enter value for 'x' and 'n':-");
scanf("%f%d",&x,&n);
sum=x;
for(i=3;i<=n;i=i+2)
{
c=1;
d1=x;
while(c
{
d1=d1*x;
c=c+1;
}
d2=1;
for(j=i;j>0;j--)
{
d2=d2*j;
}
d1=d1/d2;
counter=counter+1;
if(counter%2!=0)
sum=sum-d1;
else
sum=sum+d1;
}
printf("\nsinx=%.2f",sum);
getch();
}
Evaluate the following expression S= x + 2x^2 +3x^3+....+nx^n using for loop.
#include < stdio.h>
#include < stdio.h>
void main()
{
int i,sum=0,x,n,a=0;
clrscr();
printf("Enter the value of 'x' and 'n':-");
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
{
a++;
if(i < n)
printf("%d*%d^%d+",a,x,i);
else
printf("%d*%d^%d=",a,x,i);
sum=sum+a*pow(x,i);
}
printf("\t%d",sum);
getch();
}
#include < stdio.h>
#include < stdio.h>
void main()
{
int i,sum=0,x,n,a=0;
clrscr();
printf("Enter the value of 'x' and 'n':-");
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
{
a++;
if(i < n)
printf("%d*%d^%d+",a,x,i);
else
printf("%d*%d^%d=",a,x,i);
sum=sum+a*pow(x,i);
}
printf("\t%d",sum);
getch();
}
Another program on for loop. Evaluate the expression sin x= x-x3/3!+x5/5!-x7/7!+....
#include < stdio.h>
void main()
{
float x,t,sum,d1,d2;
int i,j,n,c,counter=0;
clrscr();
printf("Enter value for 'x' and 'n':-");
scanf("%f%d",&x,&n);
sum=x;
for(i=3;i<=n;i=i+2)
{
c=1;
d1=x;
while(c
{
d1=d1*x;
c=c+1;
}
d2=1;
for(j=i;j>0;j--)
{
d2=d2*j;
}
d1=d1/d2;
counter=counter+1;
if(counter%2!=0)
sum=sum-d1;
else
sum=sum+d1;
}
printf("\nsinx=%.2f",sum);
getch();
}
Evaluate the following expression S= x + 2x^2 +3x^3+....+nx^n using for loop.
#include < stdio.h>
#include < stdio.h>
void main()
{
int i,sum=0,x,n,a=0;
clrscr();
printf("Enter the value of 'x' and 'n':-");
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
{
a++;
if(i < n)
printf("%d*%d^%d+",a,x,i);
else
printf("%d*%d^%d=",a,x,i);
sum=sum+a*pow(x,i);
}
printf("\t%d",sum);
getch();
}
No comments:
Post a Comment