Required output:
5
454
34543
2345432
123454321
How can I do this using recursion? I have the code idea which is:
public static void main(String[] args)
{
System.out.println(func(5));
}
public static String func(int num)
{
return num + "" +meth(num-1, num, num-1);
}
public static String meth(int start, int num, int end)
{
if(start==1)
{
return "1";
}
System.out.println(start+num+end);
return meth(start-1, num, end-1);
}
I am confused about what to return in the if statement and the System.out.println(), because the number 5 won't decrease/increase as it will stay for example, it will stay 5 vertically, how can I deal with this problem? my code is more of an illustration just to prove that I am doing it.