Tutorial 13

 Tutorial 13 Code:

----Tail1 Code

// Code Showing Tail Recursion 

#include <iostream> 

using namespace std; 


void fun(int n) 

if (n > 0) 

    { 

cout << n << " "; 

fun(n - 1); 


int main() 

int x = 3; 

fun(x); 

return 0; 


-----Tail by Loop Code

// Converting Tail Recursion into Loop 
#include <iostream> 
using namespace std; 

void fun(int y) 
while (y > 0)
     { 
cout << y << " "; 
y--; 
int main() 
int x = 3; 
fun(x); 
return 0; 

----- Non Tail 1 Code:

#include <iostream> 
using namespace std; 

// Recursive function 
void fun(int n) 
if (n > 0)
     { 
fun(n - 1); 
cout << " "<< n; 

int main() 
int x = 3; 
fun(x); 
return 0; 


-------Non Tail 2 by Loop Code:

// Converting Head Recursion into Loop 
#include <iostream> 
using namespace std; 

// Recursive function 
void fun(int n) 
int i = 1; 
while (i <= n) { 
cout <<" "<< i; 
i++; 

// Driver code 
int main() 
int x = 3; 
fun(x); 
return 0; 


----- Non Tail by Loop:


// Converting Head Recursion into Loop 
#include <iostream> 
using namespace std; 

// Recursive function 
void fun(int n) 
int i = 1; 
while (i <= n) { 
cout <<" "<< i; 
i++; 

// Driver code 
int main() 
int x = 3; 
fun(x); 
return 0; 

------ Nested Recursion Code

// C++ program to show Nested Recursion 
#include <iostream> 
using namespace std; 

int fun(int n) 
if (n > 100) 
return n - 10;  
return fun(fun(n + 11)); 

// Driver code 
int main() 
int r; 
r = fun(95); 
cout << " " << r;  
return 0; 

// This code is contributed by shivanisinghss2110

--------


No comments:

Post a Comment

Fell free to write your query in comment. Your Comments will be fully encouraged.