the code below demonstates the principle of the'recursive-call' that the programing beginner may be confused with,and each demonstrate was followed by a screenshot:
the first part of the code called in the main:
String s2 = toBinary2(6, sb);
the actual code:
static String toBinary2(long n, StringBuilder sb)
{
if(n > 0)
{
toBinary2(n / 2, sb);
sb.append(n % 2);
}
return sb.toString();
}
the relevant screenshot:
figure 01 - to binary
the second part of the code called in the main:
int sum = sum(100000);
the actual code:
static int sum(int num)
{
if(num == 1)
return 1;
return num + sum(num - 1);
}
the correspond screenshot: