Menu
Question Index
...

What will be the output of the following program?

class RecursiveFor
{
    public static void main(String[] args)
    {
        int out = 15;
        for (int i = 4; i < 6; i++)
        {
            for (int j = 7; j >= 5; j--)
            {
                if (i == j)
                //break;
                if (i > j)
                {
                    out += main(i, j);
                }
                else
                {
                    out += main(j, i);
                }
            }
        }
        System.out.println("out = " + out);
    }

    private static int main(int a, int b)
    {
        if (a - b == 0)
        {
            return 2;
        }
        return a - main(a - 1, b);
    }
}


out = 78
out = 32
out = 17
Compilation Error
Goes into infinite loop

Doubts

Problems

Topic: Recursive In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App