# Thread: Ackerman Function

1. ## Ackerman Function

Q. The Ackerman recursion can be described as below: A (0, n)
= n + 1
A (m + 1, 0) = A (m, 1)
A (m + 1, n + 1) = A (m, A (m + 1, n))
Given m and n as input, write a program to calculate A(m, n), using recursion.

Code:
```
#include<stdio.h>
int Ackerman(int m,int n);
int main()
{
int m,n;
printf("Enter two numbers: ");
scanf("%d%d",&m,&n);
printf("Ackerman(%d,%d) = %d.\n",m,n,Ackerman(m,n));
return 0;
}
int Ackerman(int m,int n)
{
if(m==0)
return n+1;
if(n==0 && m>0)
return Ackerman(m-1,1);
if(m>0 && n>0)
return Ackerman(m-1,Ackerman(m,n-1));
}```

Thanks you for your constructive advices. 2. Since Ackerman recursion cannot be called with negative values:
Code:
```unsigned int ackermann(unsigned int m, unsigned int n)
{
if (!m) return n + 1;
if (!n) return ackermann(m - 1, 1);
return ackermann(m - 1, ackermann(m, n - 1));
}``` 3. Thankyou Popular pages Recent additions #### Tags for this Thread

ackerman, c programming, functions, recursion, return 