Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

Thursday, 30 November 2017

Implementation of Tower Of Hanoi recursive method in C++

 
#include<bits/stdc++.h>
using namespace std;

void towerOfHanoi(int n, char from, char to, char aux)
{
    if (n == 1)
    {
        printf("n Move disk 1 from rod %c to rod %c", from, to);
        return;
    }
    towerOfHanoi(n-1, from, aux, to);
    printf("n Move disk %d from rod %c to rod %c", n, from, to);
    towerOfHanoi(n-1, aux, to, from);
}
 
int main()
{
    int n = 4; // Number of disks
    towerOfHanoi(n, 'A', 'C', 'B');  // A, B and C are names of rods
    return 0;
}

Wednesday, 30 August 2017

Fibonacci Series Using Recursion In Python

##PYTHON PROGRAM TO FIND FIBONACCI SERIES USING RECURRSION

global sum;
sum=0
first=0;
second=1;
print(first);
print(second);
def fab(first,second):
    global sum
    if(sum>100):
        exit();
    sum=first+second;
    print(sum);
    fab(second,sum);

fab(first,second);
    

Amazon1Ads