+ 2

What will be the output of the following code..?Also plz include steps in your answer...

#include <iostream> void e(int) ; int main() { int a=3; e(a); return 0; } void e(int n) { if(n>0) { e(--n); cout <<n; e(--n); } }

19th Oct 2018, 2:18 AM
Marut Nandan
Marut Nandan - avatar
6 Answers
+ 1
Your 'extra' zero is display 7 lines from bottom because n was high enough for the line 16 call to actually enter it's if.
19th Oct 2018, 11:37 AM
John Wells
John Wells - avatar
+ 2
A compiler error as e is undefined where you call it in main.
19th Oct 2018, 4:14 AM
John Wells
John Wells - avatar
+ 2
Thanks John Wells ...
20th Oct 2018, 2:28 AM
Marut Nandan
Marut Nandan - avatar
+ 1
I have edited my code John Wells...
19th Oct 2018, 8:31 AM
Marut Nandan
Marut Nandan - avatar
19th Oct 2018, 8:38 AM
Marut Nandan
Marut Nandan - avatar
+ 1
Line 8 calls e with 3. Line 14 is true (nested e level 1). Line 16 changes n to 2 and calls e with 2 (1). Line 14 is true (2). Line 16 changes n to 1 and calls e with 1 (2). Line 14 is true (3). Line 16 changes n to 0 and calls e with 0 (3). Line 14 is false (4). Line 20 exits e (4). Line 17 displays 0 (3). Line 18 changes n to -1 and calls e with -1 (3). Line 14 is false (new 4). Line 20 exits e (4). Line 20 exits e (3). Line 17 displays 1 (2). Line 18 changes n to 0 and calls e with 0 (2). Line 14 is false (new 3). Line 20 exits e (3). Line 20 exits e (2). Line 17 displays 2 (1). Line 18 changes n to 1 and calls e with 1 (1). Line 14 is true (new 2). Line 16 changes n to 0 and calls e with 0 (2). Line 14 is false (new 3). Line 20 exits e (3). Line 17 displays 0 (2). Line 18 changes n to -1 and calls e with -1 (2). Line 14 is false (new 3). Line 20 exits e (3). Line 20 exits e (2). Line 20 exits e (1). Line 10 exits program
19th Oct 2018, 11:31 AM
John Wells
John Wells - avatar