//program to get sum of x+ x2/2+x3/3+x4/4+.............................nth terms.
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float i,n,x,m=1,sum=0;
{
float i,n,x,m=1,sum=0;
printf("enter positive number for 'n'\n");
scanf("%f",&n);
scanf("%f",&n);
printf("enter value for 'x'\n");
scanf("%f",&x);
for(i=1;i<=n;i++)
{
{
sum=sum+(pow(x,m)/i);
m=m+1;
}
printf("the sum=%f",sum);
getch();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
logics in mind:
->enter a number for range for which you want to get sum, say 'n'.
->enter a number for range for which you want to get sum, say 'n'.
->let a variable 'sum' with initial value '0'
->here we also need value for 'x' so let's input that
->we get sum using formula sum=sum+(pow(x,m)/i); because in terms x+ x2/2+x3/3+x4/4+.............. ,
->we have two components namely numerator and denominator. Let's look at numerator;they are
x1,x2,x3,x4.....it can be formulated as pow(x,m) . .here the power (m) goes on increasing.
->similarly for denominator, it is simply 1,2,3,4.... so it can be used from loop (variable i).
->similarly for denominator, it is simply 1,2,3,4.... so it can be used from loop (variable i).
->we find sum inside loop
->at last we display the final sum.
here 'pow' means finding power. it exists inside math.h
No comments:
Post a Comment