You are on page 1of 6

9.

Develop a Program to compute Sin(x) using Taylor series


approximation. Compare your result with the built- in Library
function. Print both the results with appropriate messages.

Algorithm:

Step 1: [Start]
Begin
Step 2: [Read the value of degrees]
Read degree
Step 3: [Read the number of terms in the series]
Read n
Step 3: [Initialization & Radians conversion] Temp=x
x=degree*(3.142/180.0)
term=x
sum=term

Step 4: [compute sin value]]


for i=3 through n by steps 2
term= -term*x*x / ((i-1)*i)
sum=sum+term
Step 5: [Print the output]
print degree and the sum
print sin(x)= without using library function , print degree and
sum
print sin(x)= using library function
Step 6: [Stop] End
Program:

#include<stdio.h>
#include<math.h>
void main()
{
int i,n;
float degree,x,term,sum;
printf("Enter the value in degree's : ");
scanf("%f",&degree);
printf("Enter number of terms in the series : ");
scanf("%d",&n);
x=degree*3.142/180;
term=x;
sum=term;
for(i=3;i<=n;i=i+2)
{
term=-term*x*x/((i-1)*i);
sum=sum+term;
}
printf("MYSIN(%g)=%f \n",degree,sum);
printf("Using built-in librabry function, SIN(%g)=%f \n",degree,sin(x));
}
12. Develop a program to find the square root of a given number N
and execute for all possible inputs with appropriate messages. Note:
Don’t use library function sqrt(n).

Algorithm:

Step 1: [Start]
Begin
Step 2: [Input an integer n] Read n
Step 3: [check if the number is positive]
If n<0
print number is negative
else
calculate the squareroot of n
initialize x=1.0
for i=1 through n/2 in steps 1
root=( (x*x)+n ) / (2*x);
x=root;

Step 4: [print the results]


Print the squareroot of a given number without using
library function
Print the squareroot of a given number using library
function
Step 5: [Stop]
End
Program:

#include<stdio.h>
#include<math.h>
void main()
{
int i;
float n,x,root;
printf("Enter the Number : ");
scanf("%f",&n);
if(n<0)
{
printf("Invalid Input\n");
}
else
{
x=1.0;
for(i=1;i<=30;i++)
{
root=( (x*x)+n ) / (2*x);
x=root;
}
printf("The Square Root of %f is %f \n",n,root);
printf("Using Built-In Library function,Square Root of
%f is %f\n",n,sqrt(n));
}
}

You might also like