Given two numbers, let\'s say start = 1
and end = 4
, I am trying to count all the numbers in sequence up and then down . No looping is allowed
try this
private static int CountUpAndDown(int end, int first, int start)
{
if(end==first)
{
return -1;
}
if (start > end)
{
System.out.println(--end);
}
else {
System.out.println(start++);
}
return CountUpAndDown(end, first, start);
}
You just have to use the recursion for counting up. Then, when the function returns, you are on your way down. This can be achieved with:
public void countUpAndDown(int start, int end) {
System.out.println(start);
if (end == start) return;
countUpAndDown(start+1, end);
System.out.println(start);
}
You might be able to set it to a count up from 1->3 and >=4 do a -- down to 1.