Recursive in C

Recursion is the process of repeating an ingredient in the same way. Here is a general illustration:

 void tenhamdequi () { tenhamdequi (); /* goi chinh no */ } int main () { tenhamdequi (); } 

C programming language supports recursion, for example, a function can call itself. But when you use recursive functions, programmers need to carefully define conditions to exit a function, when an infinite loop is encountered.

Recursive iteration functions are useful for solving math problems such as calculating factorials, creating Fibonacci sequences, .

Calculate factorial in C

Here is an example, which can calculate the factorial of a given number using recursive function:

 #include int tinhgiaithua ( unsigned int i ) { if ( i <= 1 ) { return 1 ; } return i * tinhgiaithua ( i - 1 ); } int main () { int i = 10 ; printf ( "Gia tri giai thua cua %d la %dn" , i , tinhgiaithua ( i )); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compile and execute the above C program to see the results:

Fibonacci sequence in C

Here's another example, creating the Fabonacci sequence for a given number using the recursive function:

 #include int day_fibonaci ( int i ) { if ( i == 0 ) { return 0 ; } if ( i == 1 ) { return 1 ; } return day_fibonaci ( i - 1 ) + day_fibonaci ( i - 2 ); } int main () { int i ; for ( i = 0 ; i < 10 ; i ++) { printf ( "%dt%n" , day_fibonaci ( i )); } printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compile and execute the above C program to see the results:

According to Tutorialspoint

Previous article: Handling errors in C

Next lesson: Variable parameter in C

4 ★ | 1 Vote

May be interested

  • 10 tips to help you become a great Pokemon trainer10 tips to help you become a great Pokemon trainer
    in the article below, tipsmake.com will introduce to you some tips to easily become a great trainer in this pokemon go game offline ...
  • How to use GoChat application in Pokémon GOHow to use GoChat application in Pokémon GO
    pokémon go has become a very hot phenomenon since its debut. any information or tricks related to the game are read and applied by players during the process of catching pokémon. and the gochat chat app for pokémon go players brings space to capture pokémon much more interesting.
  • How to play Pokemon GO in Landscape Mode on the iPhoneHow to play Pokemon GO in Landscape Mode on the iPhone
    although players can play pokemon go in portrait mode. however, if you want to watch and play games on a large and eye-catching screen, players can switch to playing games in landscape mode.
  • The secret to controlling Pokemon Go employees at workThe secret to controlling Pokemon Go employees at work
    these days, hr managers are faced with an extremely painful problem that is the status of priority employees playing pokemon go more than work. this has caused a small impact on productivity and efficiency.
  • Check out the 'buffalo' Pokémon in Pokémon GoCheck out the 'buffalo' Pokémon in Pokémon Go
    each type of pokemon has hp, cp, ability to attack and endure differently. based on these indicators, players can determine as well as choosing the most powerful pokemon for their offensive tactics.
  • Sitting home can also locate Pokemon around, do you believe it?Sitting home can also locate Pokemon around, do you believe it?
    the tightening of the niantic developers' rules to prevent players from abusing the support tools also brings annoyance, such as those who have no conditions to move much, go away, it is hard to know. get the location of the pokemon around the area they live in
  • 5 undeniable benefits when playing Pokemon Go5 undeniable benefits when playing Pokemon Go
    get to know many new people, breathe fresh air, relieve stress, increase concentration thanks to going out for a walk .... are compelling reasons to force you to try pokemon go now .
  • Want to earn the fastest Pokécoins in Pokémon Go? So don't miss this article!Want to earn the fastest Pokécoins in Pokémon Go?  So don't miss this article!
    pokécoins in pokémon go play the role of buying items in the store. the more coins you earn, the more likely you are to buy more items. to earn pokécoins, players will have to complete certain tasks or buy real money.
  • Pokémon systems when fighting in Pokémon GoPokémon systems when fighting in Pokémon Go
    each pokémon system in pokémon go has different strengths, along with a specific weakness. this type of pokémon will have the power to attack the other pokémon, but can defeat the other pokémon. if you know the characteristics of each type, it will be easier to choose which pokémon to battle.
  • The terms you need to know when playing Pokémon GoThe terms you need to know when playing Pokémon Go
    pokémon go is the most prominent name in recent days. this game of capturing and training virtual animals has created a relatively new way of playing, as players have to constantly move to catch pokémon. during the process of joining pokémon go, you will encounter and use a lot of important terms. so what do they mean?