Dec 11, 2014

Recersive Function 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;

int Array(int arr[],int n)
{
    if(n>0)
    {
        Array(arr,n-1);
        {
            cin>>arr[n-1];
        }
    }
    return arr[n-1];
}

int sum(int a[],int n)
{
int sum=0;
for (int i=0;i<n;i++)
{    sum+=a[i];
}
return sum;
}

int main()
{
    int n;
    cout<<"Enter size of Array : ";
    cin>>n;
    int x[n];
    cout<<"Enter elements: ";
    Array(x,n);
    cout<<"The array fill up by this number: ";
    for(int i=0;i<n;i++)
    {
        cout<<x[i]<<" ";
    }
    cout<<"\n"<<"The sum of all digit in array is : "<<sum(x,n);
}

No comments:

Create a Diamond in C++

 #include<iostream> using namespace std; int main() { int i, j, k,r; cout<<"Enter the row of diamond   "; cin>>r...