You are on page 1of 6

Lab No.

4 (Advance)

Name: YOGESH PARTH Dept: Avionics

Roll No. SC11B128 Sub: C Programming Lab

Question 1: program to calculate the square of all prime numbers between 1 and 10. Program code: #include<conio.h> #include<stdio.h> void main() { int a=1, sum=4, p=1; for(a=3;a<=10;a++) { int i=1; for(i=2;i<a;i++) { if(a%i==0) { p=0; break; } else p=1; } if(p==1) { sum=sum+(a*a); } } printf("The sum of squares of prime numbers from 1 to 10 is: %d",sum); getch(); }

Result: The sum of squares of prime numbers from 1 to 10 is: 87

Discussion: We are using double for loop along with break command as well as concept of prime no.

Question 2(a): Program to calculate roots of equation f(x)=X^2-5sin(x)=0 using bisection method.

Program code: #include<conio.h> #include<stdio.h> #include<math.h> float f(float x) { float val=(x*x)-5*sin(x); return val; } void main() { //the bisection method float xl, xu, xm, xm1=0, ea; xl=-0.01; xu=0.001; while(ea>=0.5) { xm=(xl+xu)/2; if(f(xl)*f(xm)<0) xu=xm; else if(f(xm)*f(xu)>0) xl=xm; ea=abs((xm-xm1)/xm); xm1=xm; } xm=(xl+xu)/2; printf("the solution up to 2 correct significant digits is: %f\n",xm); getch(); }

Result: The solution up to 2 correct significant digits is: -0.004500

Discussion: Rate of convergence is very slow and less precise. The concept of function definition turns out to be helpful in applying the conditions in the while loop.

Question 2(b):using false-position method. Program code: #include<conio.h> #include<stdio.h> #include<math.h> float f(float x){ float val=(x*x)-5*sin(x); return val; } void main() { //the false- position method float xl, xu, xm, xm1=0, ea=1; xl=-0.001; xu=0.05; while(ea>=0.5) { xm=(xu*f(xl)-xl*f(xu))/(f(xl)-f(xu)); if(f(xl)*f(xm)<0) xu=xm; else if(f(xm)*f(xu)<0) xl=xm; else if(f(xm)==0) break; ea=abs(((xm-xm1)/xm)*100); xm1=xm; } if(f(xm)!=0) { xm=(xl+xu)/2; printf("the solution up to 2 correct significant digitsis: %f\n",xm); } Else { printf("the solution up to 2 correct significant digits is: %f\n",xm); } getch(); } Result: The solution up to 2 correct significant digits is: -0.000000

Discussion: It has faster rate of convergence and more precise. The while looping statement is used.

Question 2(c):Newton-Raphson method

Program code: #include<conio.h> #include<stdio.h> #include<math.h> float f(float x) { float val=(x*x)-5*sin(x); return val; } float derf(float y) { float val=(y*2)-5*cos(y); return val; } void main() { //the newton raphson method float xi, xi1=0, ea=1; xi=0.001; while(ea>=0.5) { xi1=xi-(f(xi)/derf(xi)); ea=abs((xi1-xi)/xi1); xi=xi1; } printf("the solution up to 2 correct significant digits is: %f\n",xi1); getch(); } Result: The solution up to 2 correct significant digits is: 0.000000 Discussion: Convergence is the fastest in this method but the problem with this method is that if any inflection point or any point of local maxima or minima comes into the iteration, then we get errors.

Question 3: Program to calculate value of e^x, sin(x), cos(x) without using math.h. Program code: #include<conio.h> #include<stdio.h> float powfact(float x,int n) { float val=1; int i=1; if(n==0) return 1; else { while(i<=n) { val*=(float)(x/i); i+=1; } return val; } } float e(float x) { float diff=1,val=0,preval=0; int n=0; while(diff>=0.01){ val+=powfact(x,n); n+=1; diff=val-preval; preval=val; } return val; } float sine(float y) { float diff=1,val=0,preval=0; int n=1; while(diff>=0.01) { if(n%4==1) val+=powfact(y,n); else val-=powfact(y,n); n+=2; diff=val-preval; preval=val; } return val; }

float cosine(float z) { float diff=1,val=0,preval=0; int n=0; while(diff>=0.01) { if(n%4==0) val+=powfact(z,n); else val-=powfact(z,n); n+=2; diff=val-preval; preval=val; } return val; } void main() { float x; int i; printf("Enter x followed by 1 for e^x, 2 for sine and 3 for cosine (enter in of -pi/2 to +pi/2):"); scanf("%f%d",&x,&i); float ex=0, sinx=0, cosx=0; ex=e(x); sinx=sine(x); cosx=cosine(x); if(i==1) printf("e^x = %f\n",ex); else if(i==2) printf("sinx = %f\n",sinx); else if(i==3) printf("cosx = %f\n",cosx); getch(); } Result: Enter x followed by 1 for e^x, 2 for sine and 3 for cosine (enter in of -pi/2 to +pi/2): 11 e^x=2.716667 12 Sin(x)=0.833333 23 Cos(x)=-1.00000 Discussion: we are using while looping to calculate the value of exponential of x , sin(x), cos(x).

You might also like