Write a complete C program You will need a main function and

Write a complete C program. You will need a main function and an Ackerman function. Output should be done in main or with a third function. The Ackerman function should only calculate the appropriate Ackerman value and then return the value.

Ackermann function : http://mathworld.wolfram.com/AckermannFunction.html

A(x, y) = { y + 1 if x = 0

A (x -1), ) if y = 0

A (x - 1, A(x, y-1)) otherwise}

Solution

#include<stdio.h>

int ackermann(int,int); //function prototype

int main()
{
int x,y;
//input values of x and y
printf(\"\ Enter the value for x : \");
scanf(\"%d\",&x);
printf(\"\ Enter the value for y : \");
scanf(\"%d\",&y);
printf(\"\ A(%d , %d) = %d\",x,y,ackermann(x,y)); //call to ackermann function

return 0;
}

int ackermann(int x,int y) //recursive function to compute ackermann function
{
   int result;
   if(x==0) result=y+1;   
   else if(x!=0 && y==0) result=ackermann(x-1,1);
   else result=ackermann(x-1,ackermann(x,y-1));
   return result;
}
  

output:

Write a complete C program. You will need a main function and an Ackerman function. Output should be done in main or with a third function. The Ackerman functio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site