Wednesday, March 23, 2016

Wonder of recursive function

রিকার্সিভ ফাংশনরে আগে যদি কোন কাজ থাকে তবে রিকার্সন কাজ করার সাথে সাথে ঐ কাজ গুলাও করতে থাকে। যদি  না থাকে তবে রিকার্সনের কাজ আগে শেষ করে , তারপর রি কারশন অনুসারে রেজাল্ট দিবে।

যেমন

//Bismillah-hir-rah-ma-nir-rahim(in the name of Allah)
//Thinker: Arafat Kamal Tamzid(Sylhet Engineering College)

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

#define pf printf
#define sc scanf
#define fl float
#define pi acos(-1)


void f(int i, int a)
{
    if(i<=a)
    {

        printf("%d ", i);
        f(i+1, a);
   

    }
}

int main()
{

    int a=5,i=1;
    f(i,a);
    return 0;
}

এর output: 1 2 3 4 5

আর অন্য ভাবে
//Bismillah-hir-rah-ma-nir-rahim(in the name of Allah)
//Thinker: Arafat Kamal Tamzid(Sylhet Engineering College)

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

#define pf printf
#define sc scanf
#define fl float
#define pi acos(-1)


void f(int i, int a)
{
    if(i<=a)
    {

             f(i+1, a);
  printf("%d ", i);
   

    }
}

int main()
{

    int a=5,i=1;
    f(i,a);
    return 0;
}


এটার আউটপুট হচ্ছে: 5 4 3 2 1

No comments:

Post a Comment

Speedup Android Studio

Go to Help ->Edit Custom VM Options, and chnge this 4 setting. After that close your Android Studio. This settings are for 8gb of ram pc...