Download Recursive Functions & Examples: Factorial, Fibonacci, Arithmetic, & Library Functions and more Exercises Computer Programming in PDF only on Docsity!
Recursive Function
A recursive function is a function that call itself again!!
The recursion executes while the original call to the function is still open, i.e., it is not yet finished executing. The recursive step results in many more such calls. To stop recursion, there must be some stopping condition
Factorial Problem
void main() { int fact=1; int number; cout<<"\n\n Please enter an integer to find its factorial : "; cin>>number; for (int x=number ; x>0 ; x-- ) fact*=x; cout<<"\n\n\t\t The Factorial of " << number << " is : " << fact; }
Lab Task
Write a function to obtain the numbers of a
Fabonacci sequence. In a Fabonacci sequence
the sum of two successive terms gives the third
term. Following are the Fabonacci sequence:
Solution
void Fibo()
int i=1,j=1,sum=1,num, k = 1;
cout<<"Enter the limit for the
series =";
cin>>num;
cout << i;
cout << ",";
while(k < num)
cout << sum;
cout << ",";
i=j;
j=sum;
sum=i+j;
k = k + 1;
Lab Task
Two numbers are entered through the keyboard. Write
a program to find the value of one number raised to
power of another. (By using recursive function)
Arithmetic Manipulations
total = total + item; // adds “item” to “total”
Similarly you can use divide, multiplication, modulus
and subtraction
total += item; // adds “item” to “total”
*// demonstrates arithmetic assignment operators #include using namespace std; int main() { int ans = 27; ans += 10; //same as: ans = ans + 10; cout << ans << “, “; ans -= 7; //same as: ans = ans - 7; cout << ans << “, “; ans = 2; //same as: ans = ans * 2; cout << ans << “, “; ans /= 3; //same as: ans = ans / 3; cout << ans << “, “; ans %= 3; //same as: ans = ans % 3; cout << ans << endl; return 0; }
Prefix and Postfix
The increment operator can be used in two
ways: as a prefix, meaning that the operator
precedes the variable; and as a postfix,
meaning that the operator follows the variable.
Often a variable is incremented within a
statement that performs some other operation
on it
totalWeight = avgWeight * ++count;
Prefix and Postfix
The question here is this: Is the multiplication
performed before or after count is incremented?
In this case count is incremented first. How do
we know that? Because prefix notation is used:
++count. If we had used postfix notation,
count++, the multiplication would have been
performed first, then count would have been
incremented.
// demonstrates the increment operator #include using namespace std; int main() { int count = 10; cout << “count=”<<count << endl;//displays 10 cout << “count=”<<++count << endl;//displays 11 (prefix) cout << “count=”<<count << endl;//displays 11 cout << “count=”<<count++ << endl;//displays 11 (postfix) cout << “count=”<<count << endl;//displays 12 return 0; }
Decrement operator
The decrement operator, --, behaves very much
like the increment operator, except that it
subtracts 1 from its operand. It too can be used
in both prefix and postfix forms.
// demonstrates sqrt() library function #include //for cout, etc. #include //for sqrt() using namespace std; int main() { double number, answer; //sqrt() requires type double cout << “Enter a number: “; cin >> number; //get the number answer = sqrt(number); //find square root cout << “Square root is “ << answer << endl; //display result return 0; }
Other cmath Functions
TRY!!
ceil(x)
cos(x)
exp(x)
fabs(x)
floor(x)
log(x)
log10(x)
pow(x,y)
sin(x)
tan(x)