Table of Contents
This guide covers recursive in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.