Series
Exponential series: Exponential Series is a series which is used to find the value of
ex .
The formula used to express the ex as Exponential Series is
Expanding the above notation, the formula of Exponential Series is
For example,
Let the value of x be 3.
So, the value of e3 is 20.0855
C-programme without using factorial:
/* Program for Exponential Series */
#include<stdio.h>
#include<math.h>
void main()
{
int i, n;
float x, sm=1, tn=1;
printf("Enter the value for x : ");
scanf("%f", &x);
printf("\nEnter the value for n : ");
scanf("%d", &n);
/* Loop to calculate the value of Exponential */
for(i=1;i<=n; i++)
{
tn=tn*x/i; // to calculate the nth term of the series
sm=sm+tn; //exponential
}
printf("\nThe Exponential Value of %f = %.4f", x, sm);
}
Sine Series: Sine Series is a series which is used to find the value of Sin(x).
where, x is the angle in Radian. The formula used to express the Sin(x) as Sine
Series is
Expanding the above notation, the formula of Sine Series is
C-programme without using factorial:
#include<stdio.h>
#include<math.h>
void main()
{
int i, n;
float x, sm, t, tn;
printf(" Enter the value for t : "); // t is in degree
scanf("%f", &t);
printf(" Enter the value for n : ");
scanf("%d", &n);
x=t*3.14159/180;//convert degree to radian
/* Loop to calculate the value of Sine */
sm=x; tn=x; // tn is the nth term
for(i=1;i<=n;i++)
{
tn=(tn*(-1)*x*x)/(2*i*(2*i+1));// calculate nth term of the series
sm=sm+tn;
}
printf(" The value of Sin(%f) = %.4f",t,sm);
}
Cosine Series: Cosine Series is a series which is used to find the value of Cos(x).
where, x is the angle in Radian. The formula used to express the Cos(x) as Cosine
Series is
Expanding the above notation, the formula of Cosine Series is
C-programme without using factorial:
#include<stdio.h>
#include<math.h>
void main()
{
int i, n;
float x, t, sm, tn;
printf(" Enter the value for t : "); // t is in degree
scanf("%f",&t);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=t*3.14159/180;//convert degree to radian
/* Loop to calculate the value of Cosine */
tn=1; sm=1;
for(i=1;i<=n;i++)
{
tn=tn*(-1)*x*x/(2*i*(2*i-1));
sm=sm+tn;
}
printf(" The value of Cos(%f) is : %.4f", t, sm);
}
Factorial C-Programme:
#include <stdio.h>
#include <math.h>
void main ()
{
int i, n, fact=1;
printf("enter the value whose factorial has to be calculated:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
fact=fact * i;
}
printf("factorial of %d \n is equal to %d\n", n, fact);
}
………………………………………………………………………………………
………………………………………………………………………………………
Series using factorial
Exponential series programme:
#include <stdio.h>
#include <math.h>
int fact(int x)
{
int i,fac=1;
for(i=1;i<=x;i++)
{
fac=fac*i;
}
return fac;
}
void main()
{
float x, sm, a;
int i,n;
printf("Enter the value of x of expx series: ");
scanf("%f",&x);
printf("Enter the limit upto which you want to expand the series: ");
scanf("%d",&n);
sm=1;
for(i=1;i<=n;i++)
{
a=pow(x,i)/fact(i);
sm=sm+a;
}
printf("Exp(%0.1f) is= %f",x,sm);
}
Sine series programme:
#include <stdio.h>
#include <math.h>
int fact(int x)
{
int i,fac=1;
for(i=1;i<=x;i++)
{
fac=fac*i;
}
return fac;
}
void main()
{
float t, x, sm=0,a,b;
int i,n;
printf("Enter the value of t of sint series: ");
scanf("%f",&t);
x = t*(3.1415/180); //
printf("Enter the limit upto which you want to expand the series: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=pow(-1,(i-1));
b=pow(x,(2*i-1))/fact(2*i-1);
sm=sm+a*b;
}
printf("Sin(%0.1f): %f",t,sm);
}
Cosine series programme:
#include <stdio.h>
#include <math.h>
int fact(int x)
{
int i,fac=1;
for(i=1;i<=x;i++)
{
fac=fac*i;
}
return fac;
}
void main()
{
float x, t, sm=1,a,b;
int i,n;
printf("Enter the value of t of cost series: ");
scanf("%f",&t);
printf("Enter the limit upto which you want to expand the series: ");
scanf("%d",&n);
x = t*(3.1415/180);
for(i=1;i<=n;i++)
{
a=pow(-1,i);
b=pow(x,(2*i))/fact(2*i);
sm=sm+a*b;
}
printf("Cos(%0.1f): %f",x,sm);
}
Natural Log series: The formula used to express the log(1+x) as log Series is
𝑥2 𝑥3 𝑥4 𝑛+1
𝑥𝑛
log(1 + 𝑥) = 𝑥 − + − … (−1)
2 3 4 𝑛
#include <stdio.h>
#include <math.h>
void main()
{
float x,sm=0,a,b;
int i,n;
printf("Enter the value of x of log(1+x)) series: ");
scanf("%f",&x);
printf("Enter the limit upto which you want to expand the series: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=pow(-1,(i+1));
b=pow(x,i)/i;
sm=sm+a*b;
}
printf("Log(1+%0.2f): %f",x,sm);
Natural Log series: The formula used to express the log(1- x) as log Series is
𝑥2 𝑥3 𝑥4 𝑥𝑛
log(1 − 𝑥) = −𝑥 − − − … −
2 3 4 𝑛
#include <stdio.h>
#include <math.h>
void main()
{
float x,sm=0,b;
int i,n;
printf("Enter the value of x of log(1-x)) series: ");
scanf("%f",&x);
printf("Enter the limit upto which you want to expand the series: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
b=(-1)*(pow(x,i)/i);
sm=sm+b;
}
printf("Log(1-%0.1f): %f",x,sm);
}
………………………………………………………………………………………
……………………………………………………………………………………….
pre-processor directive, macros and function
pre-processor: begin with a hash symbol (#). Preprocessor directives are
preprocessor commands. The most common preprocessor directive is #include
#include <stdio.h>
#define PI 3.14 // #define is the pre-processor directive similarly as #include
int main()
{
float radius, area;
printf("Enter radius of circle:");
scanf("%f", &radius);
area = PI*radius*radius;
printf("Area of circle is %.3f:", area);
}
macro: #define can be used to write macro definitions also as follows. Macros are
also like function but are a bit different from that. They can also take arguments
#include <stdio.h>
#define area(r) (3.14*r*r)
int main()
{
float radius, area;
printf("Enter radius of circle:");
scanf("%f", &radius);
area = area(radius);
printf("Area of circle is %.2f:", area);
}
We have defined a macro 'area' which takes one argument that is 'r'. So, when we
called area(radius), it got replaced by 3.14*radius*radius (as area(r) is 3.14*r*r).
function:
#include <stdio.h>
float CircleArea(float r)//function to calculate area
{
float a;
a = 3.14*r*r;
float radius;
return a;
}
void main()
{
float radius, area;
printf("Enter radius of circle:");
scanf("%f", &radius);
area = CircleArea(radius);
printf("Area of circle is %.2f:", area);
}
The first difference is that macros replace codes by their value (as it replaced area(r)
with (3.14*r*r)). So, every time code will be allocated some space. This means that
every time area(r) will appear, it will be space allocated in the memory. But this is
not the case with function. Secondly, function call takes some time but macro is
defined before the actual program. So, macro is faster.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>