Number pyramid in Java The function func(int m,int n) outputs (for m = 3, n = 5):

3

34

345

34

3


I came up with this code:

void func(int m,int n)
{
  for(int i=1;i&lt;n-m+2;i++)
{   int k=i;
  int j=m;

    while(k&gt;0)
  {
    System.out.print(j);
     k--;j++;
   }
 System.out.println("");

}


for(int i=n-m;i&gt;0;i--)
{  int k=i;
  int j=m;

  while(k&gt;0)
  {
  System.out.print(j);
     k--;j++;
   }
System.out.println("");

    }

}


Is there an alternate, better way to do it? Can it be more efficient?
You can do it recursively:

void func(int m,int n) {
  if (m == n) {
    System.out.println(m);
  } else {
    func(m, n - 1);
    for (int i = m; i &lt;= n; i++) {
      System.out.print(i);
    }
    System.out.println("");
    func(m, n - 1);
  }
}