Example 11.2
Tan x and cot x values are obtained by passing different function names to the trans function.
1#include <stdio.h>2#include <math.h>3 DoubleTranDouble(*) (Double),Double(*) (Double),Double);/*function Description Statement*/4 Main ()5 {6 Doubley, V;7v = -*3.1416/180.0;8y = Tran (sin, cos, v);/*First time Call*/9printf"Tan (=%10.6f\n)", y);Teny = tran (cos, sin, v);/*Second Call*/ Oneprintf"Cot (=%10.6f\n)", y); A } - DoubleTranDouble(*F1) (Double),Double(*F2) (Double),Doublex) - { the return(*F1) (x)/(*F2) (x); -}
Example 11.3
Using recursive method to find n!
The n! can be expressed in the following mathematical relationships:
N!= 1 when N=0
n!= n * (n-1)! When n>0
1#include <stdio.h>2 intFacintN)3 {4 intT;5 if(n = =1|| n = =0)6 {7 return 1;8 }9 ElseTen { Onet = N*FAC (N-1); A returnT; - } - } the Main () - { - intm, y; -printf"Enter m:"); +scanf"%d", &m); - if(M <0) + { Aprintf"Input data Error!\n"); at } - Else - { -y =FAC (m); -printf"\n%d!=%d\n", M, y); - } in}
Example 11.4
The recursive algorithm is used to find the square root of a number a by using the iterative formula of the square root:
X1 = (x0 + a/x0)/2
1#include <stdio.h>2#include <math.h>3 DoubleMYSQRT (DoubleADoublex0)4 {5 DoubleX1;6X1 = (x0 + a/x0)/2.0;7 if(Fabs (x1-x0) >0.00001)8 {9 returnmysqrt (A, x1);Ten } One Else A { - returnX1; - } the } - Main () - { - DoubleA; +printf"Enter A:"); -scanf"%LF", &a); +printf"The sqrt of%f=%f\n", A, mysqrt (A,1.0)); A}
123
National Computer Grade Two tutorial-C language Programming _ 11th _ further discussion of functions