Can someone explain me what would be the outcome when n=3. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain me what would be the outcome when n=3.

public static void countup(int n) { if (n == 0) { System.out.println("Blastoff!"); } else { countup(n - 1); System.out.println(n); } }

3rd May 2020, 7:46 AM
Harsh Harshana
5 Answers
+ 1
The output is 2 but you can test youself you code on the java console Think to it the next time
3rd May 2020, 8:12 AM
HEUBA BATOMEN Franck Duval
HEUBA BATOMEN Franck Duval - avatar
0
Okay...Thank you, now i have mentioned in the tags
3rd May 2020, 7:57 AM
Harsh Harshana
0
Heuba Batomen i am sorry but output is Blastoff! 1 2 3 What i dont understand is why is there 1,2 and 3 in the output
3rd May 2020, 8:22 AM
Harsh Harshana
0
Let's check the coutup documentation the awser is there It's easy
3rd May 2020, 8:24 AM
HEUBA BATOMEN Franck Duval
HEUBA BATOMEN Franck Duval - avatar
0
Why output 1 2 3. ???? Because when "n" is 0 , The value of the argument "n" in the countup(n-1) is "1". it will print 1. similarly when it return back to the calling function the value of " n" is "2". So it will print 2. ---------------------------------------- when if(0==0) print BasOff countup(1-1) when return back "n" is "1" print "1" countup(2-1) when return back "n" is "2" print "2" countup(3-1) when return back "n" is "3" print "3" Ok.
3rd May 2020, 8:38 PM
Dasarath Singh