Specify the output of the following code if the user enters 7

Question:

void function_A(int x,int y){

    int i;

    for(i=1;i<=x;i++)

    cout<<y;

}

void function_B(int n)

{

    if(n!=0)

    {

        function_A(n, n);

        cout<<endl;

        function_B(n-1);

    }

}

 

Explanation:

When the user enters 7, it is allocated to the variable x and handed to the function B, which is then invoked in the main.
For each n in 7, 6, 5, 4, 3, 2, 1, the function will be called 7 times recursively. When the value of ‘n’ reaches 0, the iteration comes to an end.
The function A is called within each iteration, and the values n, n are supplied to it. When the value of n is 7, function A prints the value 7 seven times.

Similarly, if the number of n is 6, the value 6 is printed 6 times, if it is 5, then 5 is printed 5 times, and so on until 1.

As a result, the pattern printed when the user enters 7 is:

7777777

666666

55555

4444

333

22

1

Code:

#include <iostream>
using namespace std;
void function_A(int a, int b);
void function_B(int n);

void function_A(int a,int b){
    int i;
    for(i=1;i<=a;i++)
    cout<<y;
}

void function_B(int n)
{
    if(n!=0)
    {
        function_A(n, n);
        cout<<endl;
        function_B(n-1);
    }
}

int main()
{
    int a;
    cin>>a;
    function_B(a);
    return 0;
}

Output:

user enters 7

 

Also, read the Given below-defined UML class diagram.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *