SlideShare a Scribd company logo
RECURSION CONCEPT BY DIVYA
Recursion
Recursive functions are those functions which call
themselves directly or indirectly.
Few examples which uses recursive functions are:
1. Calculation of factorial of a number
2. Fibonacci series
3. Printing values of nodes of single linked list in reverse order
4. Tower of Hanoi
5. Tree traversal( Preorder, Postorder, Inorder)
6. Depth First Search of Graph
How recursion works?
Whenever we try to solve a problem, by representing it
in one or more smaller sub problems, it is used. To stop
the recursion a base condition is used.
The memory allocation is done to a function on a stack,
whenever it is called from main() function.
A recursive function calls itself, the memory for a called
function is allocated on top of memory allocated to
calling function and different copy of local variables is
created for each function call. When the base case is
reached, the function returns its value to the function by
whom it is called and memory is de-allocated and the
process continues.
RECURSION CONCEPT BY DIVYA
Calculation of factorial of a number
#include<stdio.h>
int fact(int n)
{
if (n <= 1) // base case
return 1;
else
return n*fact(n-1);
}
int main()
{
int num,X;
printf("Enter the number ");
scanf("%d",&num);
X=fact(num);
printf("nfactorial of the number %d is %d",num,X);
return 0;
}
How it works if num=3
RECURSION CONCEPT BY DIVYA
X=fact(3)
=3 * fact(2)
=3 * 2 * fact(1)
=3 * 2 * 1
Printing values of nodes of single linked list in reverse
order
#include<stdio.h>
#include<stdlib.h> /*free( ), malloc( )*/
struct node{
int info;
struct node *link;
}*head=NULL;
void create_list(int n)
{
struct node *N=NULL;
struct node *M=NULL;
int i;
for(i=0;i<n;i++)
{
if(head==NULL)
{
N=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in 1st node");
scanf("%d", &N->info);
N->link=NULL;
head=N;
}
else
RECURSION CONCEPT BY DIVYA
{
M=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in %d node",(i+1));
scanf("%d", &M->info);
M->link=NULL;
N->link=M;
N=M;
}
}
}
void display_list()
{
struct node *temp=head;
while(temp!=NULL)
{
printf(" %d ",temp->info);
temp=temp->link;
}
}
void reverse_list(struct node *temp)
{
if(temp==NULL)
return;
reverse_list(temp->link);
printf(" %d ",temp->info);
}
int main()
{
int num_nodes;
printf("Enter the number of nodes you want to create");
scanf("%d",&num_nodes);
create_list(num_nodes);
RECURSION CONCEPT BY DIVYA
printf("Output");
display_list();
printf("nList in reverse ordern");
reverse_list(head);
return 0;
}
STEP WISE DEPICTION AFTER EXECUTION OF
RECURSIVE FUNCTION :reverse_list()
1.
RECURSION CONCEPT BY DIVYA
RECURSION CONCEPT BY DIVYA
RECURSION CONCEPT BY DIVYA
Print all the node’s info of a circular single
linked list in reverse order:
#include<stdio.h>
#include<stdlib.h> /*free( ), malloc( )*/
struct node{
int info;
struct node *link;
}*head=NULL,*last;
void create_list(int n)
{
struct node *N=NULL;
struct node *M=NULL;
int i;
for(i=0;i<n;i++)
RECURSION CONCEPT BY DIVYA
{
if(head==NULL)
{
N=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in 1st node");
scanf("%d", &N->info);
if(n==1) /*if user wants to create only one node*/
{
N->link=N;/*as it is circular list the node's link will point to
node itself*/
last=N;
}
else
N->link=NULL;
head=N;
}
else
{
M=(struct node*)malloc(sizeof(struct node));
printf("Enter the value in %d node",(i+1));
scanf("%d", &M->info);
if(i==n-1)/*when last node of list is created*/
{
M->link=head;
last=M; /* assign last pointer the address of node*/
}
else
{
M->link=NULL;
}
N->link=M;
N=M;
}
}
}
void display_list()
RECURSION CONCEPT BY DIVYA
{
struct node *temp=head;
while(temp!=NULL)
{
printf(" %d ",temp->info);
if(temp==last) /*when last node of list is reached*/
{
printf("n verify the list is circular linked list");
printf("nvalue of info of last node's link
node %d",last->link->info);
break;
}
temp=temp->link;
}
}
int main()
{
int num_nodes;
printf("Enter the number of nodes you want to create");
scanf("%d",&num_nodes);
create_list(num_nodes);
printf("Output");
display_list();
printf("nprint elements in reversen");
reverse_list(head);
RECURSION CONCEPT BY DIVYA
return 0;
}

More Related Content

What's hot

Function recap
Function recapFunction recap
Function recap
alish sha
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
Syed Umair
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
SANTOSH RATH
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
AAKASH KUMAR
 
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
AAKASH KUMAR
 

What's hot (20)

week-16x
week-16xweek-16x
week-16x
 
Function recap
Function recapFunction recap
Function recap
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Stack - Data Structure
Stack - Data StructureStack - Data Structure
Stack - Data Structure
 
Matlab project
Matlab projectMatlab project
Matlab project
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
Array notes
Array notesArray notes
Array notes
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Link list part 2
Link list part 2Link list part 2
Link list part 2
 
Arrays
ArraysArrays
Arrays
 

Similar to Recursion concepts by Divya

coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
tienlivick
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 

Similar to Recursion concepts by Divya (20)

Function in C program
Function in C programFunction in C program
Function in C program
 
functions
functionsfunctions
functions
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
Learn Function The Hard Way
Learn Function The Hard WayLearn Function The Hard Way
Learn Function The Hard Way
 
Functions
FunctionsFunctions
Functions
 
Recursion(Advanced data structure)
Recursion(Advanced data structure)Recursion(Advanced data structure)
Recursion(Advanced data structure)
 
C function
C functionC function
C function
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
7 functions
7  functions7  functions
7 functions
 
Pointer
PointerPointer
Pointer
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function in c
Function in cFunction in c
Function in c
 
Function in c
Function in cFunction in c
Function in c
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Function in c program
Function in c programFunction in c program
Function in c program
 
(Recursion)ads
(Recursion)ads(Recursion)ads
(Recursion)ads
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions & Recursion
Functions & RecursionFunctions & Recursion
Functions & Recursion
 

Recently uploaded

RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
Atif Razi
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
Kamal Acharya
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 

Recently uploaded (20)

RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Fruit shop management system project report.pdf
Fruit shop management system project report.pdfFruit shop management system project report.pdf
Fruit shop management system project report.pdf
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 

Recursion concepts by Divya

  • 1. RECURSION CONCEPT BY DIVYA Recursion Recursive functions are those functions which call themselves directly or indirectly. Few examples which uses recursive functions are: 1. Calculation of factorial of a number 2. Fibonacci series 3. Printing values of nodes of single linked list in reverse order 4. Tower of Hanoi 5. Tree traversal( Preorder, Postorder, Inorder) 6. Depth First Search of Graph How recursion works? Whenever we try to solve a problem, by representing it in one or more smaller sub problems, it is used. To stop the recursion a base condition is used. The memory allocation is done to a function on a stack, whenever it is called from main() function. A recursive function calls itself, the memory for a called function is allocated on top of memory allocated to calling function and different copy of local variables is created for each function call. When the base case is reached, the function returns its value to the function by whom it is called and memory is de-allocated and the process continues.
  • 2. RECURSION CONCEPT BY DIVYA Calculation of factorial of a number #include<stdio.h> int fact(int n) { if (n <= 1) // base case return 1; else return n*fact(n-1); } int main() { int num,X; printf("Enter the number "); scanf("%d",&num); X=fact(num); printf("nfactorial of the number %d is %d",num,X); return 0; } How it works if num=3
  • 3. RECURSION CONCEPT BY DIVYA X=fact(3) =3 * fact(2) =3 * 2 * fact(1) =3 * 2 * 1 Printing values of nodes of single linked list in reverse order #include<stdio.h> #include<stdlib.h> /*free( ), malloc( )*/ struct node{ int info; struct node *link; }*head=NULL; void create_list(int n) { struct node *N=NULL; struct node *M=NULL; int i; for(i=0;i<n;i++) { if(head==NULL) { N=(struct node*)malloc(sizeof(struct node)); printf("Enter the value in 1st node"); scanf("%d", &N->info); N->link=NULL; head=N; } else
  • 4. RECURSION CONCEPT BY DIVYA { M=(struct node*)malloc(sizeof(struct node)); printf("Enter the value in %d node",(i+1)); scanf("%d", &M->info); M->link=NULL; N->link=M; N=M; } } } void display_list() { struct node *temp=head; while(temp!=NULL) { printf(" %d ",temp->info); temp=temp->link; } } void reverse_list(struct node *temp) { if(temp==NULL) return; reverse_list(temp->link); printf(" %d ",temp->info); } int main() { int num_nodes; printf("Enter the number of nodes you want to create"); scanf("%d",&num_nodes); create_list(num_nodes);
  • 5. RECURSION CONCEPT BY DIVYA printf("Output"); display_list(); printf("nList in reverse ordern"); reverse_list(head); return 0; } STEP WISE DEPICTION AFTER EXECUTION OF RECURSIVE FUNCTION :reverse_list() 1.
  • 8. RECURSION CONCEPT BY DIVYA Print all the node’s info of a circular single linked list in reverse order: #include<stdio.h> #include<stdlib.h> /*free( ), malloc( )*/ struct node{ int info; struct node *link; }*head=NULL,*last; void create_list(int n) { struct node *N=NULL; struct node *M=NULL; int i; for(i=0;i<n;i++)
  • 9. RECURSION CONCEPT BY DIVYA { if(head==NULL) { N=(struct node*)malloc(sizeof(struct node)); printf("Enter the value in 1st node"); scanf("%d", &N->info); if(n==1) /*if user wants to create only one node*/ { N->link=N;/*as it is circular list the node's link will point to node itself*/ last=N; } else N->link=NULL; head=N; } else { M=(struct node*)malloc(sizeof(struct node)); printf("Enter the value in %d node",(i+1)); scanf("%d", &M->info); if(i==n-1)/*when last node of list is created*/ { M->link=head; last=M; /* assign last pointer the address of node*/ } else { M->link=NULL; } N->link=M; N=M; } } } void display_list()
  • 10. RECURSION CONCEPT BY DIVYA { struct node *temp=head; while(temp!=NULL) { printf(" %d ",temp->info); if(temp==last) /*when last node of list is reached*/ { printf("n verify the list is circular linked list"); printf("nvalue of info of last node's link node %d",last->link->info); break; } temp=temp->link; } } int main() { int num_nodes; printf("Enter the number of nodes you want to create"); scanf("%d",&num_nodes); create_list(num_nodes); printf("Output"); display_list(); printf("nprint elements in reversen"); reverse_list(head);
  • 11. RECURSION CONCEPT BY DIVYA return 0; }