Showing posts with label recursive function. Show all posts
Showing posts with label recursive function. Show all posts

Sunday, 2 July 2017

C++ program of binary search

#include<iostream>
#include<conio.h>
using namespace std;
int a[10];
void binary(int p, int r, int data)
{
        
         int q;
         q=(p+r)/2;
        
         if(a[q]==data)
         {
                 cout<<"found";
         }
         else if(data<a[q])
         {
                 binary(p,q-1,data);
         }
         else
         {
                 binary(q+1,r,data);
         }
        
}

int main()
{
         int d;

        
         for(int i=0;i<10;i++)
         {
                 cout<<"Enter data at ["<<i<<"] : ";
                 cin>>a[i];
         }
         cout<<"Enter data to find : ";
         cin>>d;
        
         binary(0,9,d);
}


OUTPUT:
Share:

Saturday, 20 May 2017

Recursive function of exponential

In this program we will see how a function calls itself inside its body of fuction.

Program:
#include<iostream>
#include<conio.h>

using namespace std;

int rf(int n,int b)
{
          if(b==1)
          {
                    return n;
                    }
          else 
          {
                    return n*rf(n,b-1);
          }
                   
}

int main()
{
          int m,o;
         
          cout<<"Enter base : ";
          cin>>m;
          cout<<"Enter power: ";
          cin>>o;
         
          cout<<rf(m,o);
          getch();
}


//Code Ends

Screenshot:

Share: