McCarthy 91 function in C++

0
844
Finding Greatest Common Divisor of 2 numbers in C++
Recursive factorial function in C++

McCarthy 91 function is a recursive function used within computer science, its definition is:

McCarthy 91

Now how we implement this McCarthy 91 function in our C++ code,  have a look at the below code:

    //McCarthy 91 Function
    #include<iostream>
     
    using namespace std;
     
    int M(int n)
    {
        if(n>100)
            return(n–10);
        else
            return(M(M(n+11))); //answer is 91 upto 101
     
    }
    int main()
    {
        int num;
     
        cout<<“enter a number “;
        cin>>num;
     
        int m = M(num);
        cout<<m<<endl;
     
    }

Now in the above function M() we implemented the mathematical form of the McCarthy 91 function by using recursion because recursion is its easiest approach, we used the number taken as an argument and asked if number is greater than 100 then return number-10 else return the recursive statement as defined in the definition, and it returns the value  according to McCarthy 91 function.

Finding Greatest Common Divisor of 2 numbers in C++
Recursive factorial function in C++

Leave a Reply