Recursive Function In C : Fibonacci, Factorial and Power of Numbers

C Language Flag / Logo

These are some example programs in the C language using recursive function to calculate Fibonacci, Factorial and Power of numbers. You can use a loop, but for a purpose of giving you an example on how to use recursive so we are going to use recursive instead.

Fibonacci

This is to calculate the sum of fibonacci numbers that are less than the specified number user input.
int fibonacci(int number) {
   if(number == 0){
       return 0;
   } else if(number == 1) {
       return 1;
   } else {
       return (fibonacci(number - 1) + fibonacci(number - 2));
   }
}

- Our function accept integer, then we compare them using if statement
- If the parameter number equal to 0, then the return of function is 0
- Also If the parameter number equal to 1, then the return of function is 1
- But if the number is not 1 or 0, this function will call itself with a new parameter for the first call is the parameter -1 and the second parameter -2, then add both results.
- This function will recurse itself until parameter number reach 1 or 0

Factorial

We define our function called factor that accept an integer parameter called number.
int factor(int number) {
   if (number >= 1) {
       return number * factor(number - 1);
   }else{
       return 1;
   }
}

- If the parameter number is greater than or equal to 1 we return number than calling this function again but the new parameter is the previous parameter minus 1.
- So the parameter that passes to the factor function is less and less until it reaches 0, if so we don’t need to recurse the function, instead just return 1.
- The factorial of a negative number doesn't exist. And, the factorial of 0 is 1.
- This function will recurse itself until parameter number 0.

Power of numbers

In C there's a function named pow that you can use to do this operation, but we are gonna implement it by ourselves for learning purposes using a recursive method.
int powerof(int base, int exp){
   if(exp == 0){
       return 1;
   }else{
       return base * powerof(base, exp - 1);
   }
}

- Unlike two of this previous function, this function requires two parameter, for base number and its exponent.
- This function will recurse itself until parameter exp reach 0, if the exponent is 0 then return value is 1.
- If the exponent is greater than 0, recurse the function and return it with base multiplier by calling the function with a new exponent parameter subtract by 1.

And this is the full program that you can execute to do those operations continuously just like your calculator. You can modify the program or add new features to improve the program, etc.


Some of Advantages of recursive function

- Recursion can add clarity and reduce the time needed to write and read the code, but it doesn’t mean it’s gonna make the program run faster.
- Reduces time complexity.
- Performs better in solving problems based on tree structures.

I have been doing a lot of C coding lately, not for professional use but rather just a hobby, because i like to learn a lot of stuff about programming. I think that C is a very important language because it is used everywhere on level coding programs. I have been once using C in a real project, but that was not so much of coding so then i didn’t quite pro at C.

For a beginner, learning C maybe is not the right way because you don’t get a lot of fancy stuff like in higher level language. In C there’s not so much syntax to do many stuff, so you may need to do it yourself.

Comments

Popular posts from this blog

How To Uninstall Angular CLI, The Angular Framework

ASUS Battery Health Charging Software To Make Your Battery Life Longer

How To Create Flutter Card With Example Code

Flutter Button With Left Align Text and Icon

Is Nox Player a Malware? Nox Android Emulator