10.12.14

Recursion in C++

Create a recursive c++ function to fill up an array and make sum of all digit. you have to create different recursive function for fill up and to make sum.


#include<iostream>
using namespace std;

void array_recursive(int array[],int n)
{
     if (n-1>=0)
          {
               cout<<"Enter elements into the array:\n";
               cin>>array[n];
               n--;
               array_recursive(array,n);
           }

}

int sum_array(int array[],int n)
{
            if (n= =0)
                 {
                       return array[0] ;
                  }
            return array[n]+sum_array(array,n-1);
}

int main( )
{
         int n;
         cout<<"Enter the size of an array:\n";
         cin>>n;
         int array[n];
         array_recursive(array,n);
         cout<<"Printing Recursive Data\n";
         for(int i=n; i>0; i--)
             {
                  cout<<array[i]<<"\n";
             }
         cout<<"the sum is: "<<sum_array(array+1,n-1)<<endl;
}

18 comments:

  1. Thanks for the information. You can more information on recursive Function in cpp from the following site:

    Learn Cpp online

    ReplyDelete
    Replies
    1. Dear Santhi P,
      This is a technical blog, only for programmers. No theoretical concepts is allowed here. we considered it as a prerequisite to practice here. anyway you most welcome here. Thanks for visit.

      Delete
  2. Dear Sir, Please visit my blog:
    http://itlearn24.blogspot.com/2014/12/recersive-function-in-c_11.html

    ReplyDelete
    Replies
    1. the sum function is not done here by recursion as it was mentioned in the question

      Delete
    2. Noted & checked. There must be two recursive function as per condition mentioned in the question/problem.

      Delete
  3. #include
    using namespace std;

    void array_recursive(int array[],int n)
    {
    if(n-1>=0)
    {
    cout<<"Enter elements into the array:\n";
    cin>>array[n];
    n--;
    array_recursive(array,n);
    }

    }

    int sum_array(int array[],int n)
    {
    if(n==0)
    {
    return array[0] ;
    }
    return array[n]+sum_array(array,n-1);
    }
    int main()
    {
    int n;
    cout<<"Enter the size of an array:\n";
    cin>>n;
    int array[n];
    array_recursive(array,n);
    cout<<"Printing Recursive Data\n";

    for(int i=n; i>0; i--)
    {
    cout<<array[i]<<"\n";

    }
    cout<<"the sum is: "<<sum_array(array+1,n-1)<<endl;

    }

    ReplyDelete
  4. //Name : Bikash Chandra Sutrodhor
    // ID : 201410108
    //Batch : 45 th

    #include
    class power
    {
    public:
    inline int square(int n)
    {
    -----------------------------
    -----------------------------
    }
    inline int cube(int n)
    {
    -----------------------------
    -----------------------------
    }

    };
    void main()
    {
    -------------------------------
    -------------------------------
    }

    ReplyDelete
  5. //Name : Bikash Chandra Sutrodhor
    // ID : 201410108
    //Batch : 45 th

    #include
    #include
    class line
    {
    public:
    inline float multi(float f,float h)
    {
    return(f*h);
    }
    inline float cube(float f)
    {
    return(f*f*f);
    }
    };
    void main()
    {
    line obj;
    float f1,h1;
    clrscr();
    cout<<"\n Enter Two Value ";
    cin>>c1>>d1;
    cout<<" Multiplication is :"<<obj.multi(f1,h1)<<endl;
    cout<<" Cube of val1 is :"<<obj.cube(f1)<<endl;
    cout<<" Cube of val2 is :"<<obj.cube(h1)<<endl;
    getch();
    }

    ReplyDelete
  6. //Name : Bikash Chandra Sutrodhor
    // ID : 201410108
    //Batch : 45 th

    ?#?include?
    using namespace std;
    main()
    {
    float SI=0,p,t,r;
    cin>>p>>t>>r;
    SI=((p*t*r)/100);
    cout<
    using namespace std;
    float interest(float p,float q,float r);
    main()
    {
    float p,q,SI=0;
    cin>>p>>q>>r;
    interest(p,q,r);
    }
    float interest(float u,float v,float w)
    {
    float SI;
    SI=((u*v*w)/100);
    cout<<SI;
    }

    ReplyDelete
  7. //Name : Bikash Chandra Sutrodhor
    // ID : 201410108
    //Batch : 45 th

    #include
    Float area(int r)
    {
    cout<>r;area(r);
    cout<>b>>h;
    area(b,h);
    cout<>l>>br;
    area(l,br);
    getch();
    return 0;
    }

    ReplyDelete
  8. //Name : Bikash Chandra Sutrodhor
    // ID : 201410108
    //Batch : 45 th

    #include
    class power
    {
    public:
    inline int square(int t)
    {
    return t*t;
    }
    inline int cube(int t)
    {
    return t*t*t;
    }
    };
    void main()
    {
    int t,r;
    power p;
    clrscr();
    cout<<“\nEnter the Number: \n” ;
    cin>>n;
    r=p.square(s);
    cout<<“\nSquare of “<<t<<” = “<<r<<endl;
    r=p.cube(s);
    cout<<“\nCube of “<<t<<” = “<<r<<endl;
    getch();
    }

    ReplyDelete
  9. //Name : Bikash Chandra Sutrodhor
    // ID : 201410108
    //Batch : 45 th

    ?#?include?
    using namespace std;
    //base class
    class strange
    {
    public:
    void setdigit(int d)
    {
    digit=d;
    }
    protected:
    int digit;
    };
    //derived class
    class number : public strange
    {
    public:
    int i,j,t=1,d;
    int devide()
    {
    while(d!=0)
    {
    j=d%10;
    d=d/10;
    for(i=2;i>r;
    nmb.setdigit(r);
    nmb.check();
    }

    ReplyDelete
  10. //Name : Bikash Chandra Sutrodhor
    // ID : 201410108
    //Batch : 45 th

    #include
    #include
    using namespace std;
    class CheckStrnge
    {
    public:
    int strange (int s)
    {
    int r,n,p=1,i;
    while(s!=0)
    {
    r=s%10;
    n=s/10;
    for(i=2 ;i>number;
    Str.strange(number);
    }

    ReplyDelete
  11. #include
    using namespace std;
    void array_recursive(int a[],int p)
    {
    if(p-1>=0)
    {
    cout<<"Enter elements into the array:\n";
    cin>>a[p];
    p--;
    array_recursive(a,p);
    }

    }

    int sum_array(int a[],int p)
    {
    if(p==0)
    {
    return a[0] ;
    }
    return a[p]+sum_array(a,p-1);
    }
    int main()
    {
    int p;
    cout<<"Enter the size of an array:\n";
    cin>>p;
    int a[p];
    array_recursive(a,p);
    cout<<"Printing Recursive Data\n";

    for(int j=p; j>0; j--)
    {
    cout<<a[j]<<"\n";

    }
    cout<<"the sum is: "<<sum_array(a+1,p-1)<<endl;

    }

    ReplyDelete
  12. Thanks for the information really great work
    For more information about recursive function.
    C++

    ReplyDelete
  13. I had always some unclear points in Recursive functions but now I can say with confidence that I know about recursive functions.
    Learn C++ in Urdu

    ReplyDelete
  14. To get more tutorials learning "c programming " follow this link http://www.cprogramming-bd.com/tutorial/c_tutorial.aspx

    ReplyDelete

Comment Here