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;
}

Inheritence in c++ to generate a fibonacci series

Generate a Fibonacci series between 0 and 1000. The first two initial value are 0 and 1


#include<iostream>
using namespace std;
class fibonacci
{
     public:
     void set_digit (int m, int n)
         {
              f1=m;
              f2=n;
          }
      protected:
           int f1;
           int f2;
           int f3;
};

class print : public fibonacci
{
     public:
          void show_print()
              {
                   int maxnum;
                   cout <<"Enter the maximum number of fibonacci series: "<< "\n";
                   cout <<"\nPrinting fibonacci series from 0 to 1000: " << "\n");
                   while (f1+f2 < = 1000)
                   {
                        f3=f1+f2;
                        cout <<f3 <<"\n";
                        f1=f2;
                        f2=f3;
                   }
              }
};
int main()
{
      print p;
      p.set_digit (0, 1);
      p.show_print( );
}

Constructor in c++ to Sum of all multipliers by given range

write a c++ program which contains a constructor to make sum of all multipliers of 7 or 9 between 1 and 100.


#include<iostream>
using namespace std;
class divis
{
      public:
            int sum=0;
            divis();
            ~divis();
            void result();
};

divis :: divis()
{
    int a=1;
    while(a<100)
    {
        if ( (a%7==0) || (a%9==0) )
            {
                cout << a <<"\n";
                sum = sum+a;
            }
        a++;
    }
}

divis :: ~divis()
{
    cout <<"memory released" << "\n";
}

void divis :: result()
{
    cout << "\n The sum of all divisibles: "<< sum << endl;
}

int main( )
{
     divis vs;
     vs.result();
}