SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
Recursion in C
Recursion is the mechanism of repeating objects in a similar manner to themselves. When a program
allows you to call a function inside the same function in programming languages, that function is called a
recursive call. Recursion can result in an intuitive, very simple, elegant code to follow. It may also
require the use of a very large amount of memory if the recursion is too long.
A function which itself calls is known as a recursive function. And, the strategy is called recursion.
Recursion can not be extended to all of the problem, but for the tasks that can be described in terms of
similar subtasks it is more useful. For example, recursion can be applied to problem sorting, searching,
and traversing.
C program helps you to do such function calling inside a specific function, i.e., a recursion. However
when introducing this definition of recursion, you must be careful to specify an exit or terminate
condition from this recursive function, or else it will lead to an infinite loop, so make sure the condition
is set within your program.
Example Syntax of Recursive Function in C
void rec_prog(void) {
rec_prog(); /* function calls itself */}
int main(void) {
rec_prog();
return 0;
}
Note: We need to describe correct exit condition in a recursive function to prevent indefinitely recursive
calling.
Example of recursive function in C programming : C program that uses iteration to identify the fact of
the first 6 natural numbers
#include <stdio.h>
#include<conio.h>
long int fact( int n )
{
if ( n <= 1 )
return 1;
else //here is recursive step
return ( n * fact (n-1) );
}
int main ()
{
int i;
for ( i = 1; i <=6; i++ )
printf("%d! = %dn",i, fact(i) );
return 0;
}
Output:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
Fibonacci series
#include<stdio.h>
#include<conio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
Output:
Enter the number of elements: 5
01123
Fibonacci series using Recursion in C programming
#include<stdio.h>
#include<conio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}
Output:
Enter the number of elements: 4
01123

Mais conteúdo relacionado

Mais procurados

A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
malasumathi
 

Mais procurados (20)

Pointers in C language
Pointers  in  C languagePointers  in  C language
Pointers in C language
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
Lab 1
Lab 1Lab 1
Lab 1
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Activities on Operands
Activities on OperandsActivities on Operands
Activities on Operands
 
C rules
C rulesC rules
C rules
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 
Computer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlabComputer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlab
 
C++ Question
C++ QuestionC++ Question
C++ Question
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
A1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_solA1 spyder variables_operators_nptel_pds1_sol
A1 spyder variables_operators_nptel_pds1_sol
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
20BCE1734.pdf
20BCE1734.pdf20BCE1734.pdf
20BCE1734.pdf
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 

Semelhante a Recursion

function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 

Semelhante a Recursion (20)

Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Function in c
Function in cFunction in c
Function in c
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Tail recursion
Tail recursionTail recursion
Tail recursion
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C function
C functionC function
C function
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Function in c program
Function in c programFunction in c program
Function in c program
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Presentation 2.pptx
Presentation 2.pptxPresentation 2.pptx
Presentation 2.pptx
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Último (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Recursion

  • 1. Recursion in C Recursion is the mechanism of repeating objects in a similar manner to themselves. When a program allows you to call a function inside the same function in programming languages, that function is called a recursive call. Recursion can result in an intuitive, very simple, elegant code to follow. It may also require the use of a very large amount of memory if the recursion is too long. A function which itself calls is known as a recursive function. And, the strategy is called recursion. Recursion can not be extended to all of the problem, but for the tasks that can be described in terms of similar subtasks it is more useful. For example, recursion can be applied to problem sorting, searching, and traversing. C program helps you to do such function calling inside a specific function, i.e., a recursion. However when introducing this definition of recursion, you must be careful to specify an exit or terminate condition from this recursive function, or else it will lead to an infinite loop, so make sure the condition is set within your program. Example Syntax of Recursive Function in C void rec_prog(void) { rec_prog(); /* function calls itself */} int main(void) { rec_prog(); return 0; } Note: We need to describe correct exit condition in a recursive function to prevent indefinitely recursive calling.
  • 2. Example of recursive function in C programming : C program that uses iteration to identify the fact of the first 6 natural numbers #include <stdio.h> #include<conio.h> long int fact( int n ) { if ( n <= 1 ) return 1; else //here is recursive step return ( n * fact (n-1) ); } int main () { int i; for ( i = 1; i <=6; i++ ) printf("%d! = %dn",i, fact(i) ); return 0; } Output: 1! = 1 2! = 2 3! = 6 4! = 24
  • 3. 5! = 120 6! = 720 Fibonacci series #include<stdio.h> #include<conio.h> int main() { int n1=0,n2=1,n3,i,number; printf("Enter the number of elements:"); scanf("%d",&number); printf("n%d %d",n1,n2);//printing 0 and 1 for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } return 0; } Output: Enter the number of elements: 5
  • 4. 01123 Fibonacci series using Recursion in C programming #include<stdio.h> #include<conio.h> void printFibonacci(int n){ static int n1=0,n2=1,n3; if(n>0){ n3 = n1 + n2; n1 = n2; n2 = n3; printf("%d ",n3); printFibonacci(n-1); } } int main(){ int n; printf("Enter the number of elements: "); scanf("%d",&n); printf("Fibonacci Series: "); printf("%d %d ",0,1); printFibonacci(n-2);//n-2 because 2 numbers are already printed return 0; }
  • 5. Output: Enter the number of elements: 4 01123