Can anyone explain it?? Why output is 5? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

Can anyone explain it?? Why output is 5?

// Created by Rashed //how this is output 5 public class Program { public static int f(int x){ if (x==0) return 2; else return 1 + f(x-1); } public static void main(String[] args) { System.out.println(f(3)); } }

16th Jun 2020, 4:39 AM
The Black Hat 🇨🇦🇧🇩
The Black Hat  🇨🇦🇧🇩 - avatar
2 Respuestas
+ 2
Print the return for each instance and it will be clear. f(0) = 2; f(1) = f(0)+1; f(2) = f(1)+1; f(3)=f(2)+1
16th Jun 2020, 4:51 AM
JME
0
Thanks bro..JME
16th Jun 2020, 4:56 AM
The Black Hat 🇨🇦🇧🇩
The Black Hat  🇨🇦🇧🇩 - avatar