/* C program to simulate recursion of following function 1.Factorial
2. Power Function
*/
#include <stdio.h>
int fact(int n)
{
int stack[10],top,val;
top=-1;
while(n>0)
{
stack[++top]=n; //push()
n=n-1;
}
val=1;
while(top!=-1) //stack is not empty
{
val=val*stack[top--];
}
return(val);
}
int power(int x,int n)
{
int stack[10],top,val;
top=-1;
while(n>0)
{
stack[++top]=n; //push()
n=n-1;
}
val=1;
while(top!=-1) //stack is not empty
{
val=val*x;
top--;
}
return(val);
}
int main()
{
int op,n,x;
do
{
printf("\n1)Factorial\n2)Power function\n3)Quit");
printf("\nEnter Your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter n :");
scanf("%d",&n);
printf("\nFactoial =%d",fact(n));
break;
case 2: printf("\nEnter x and n : ");
scanf("%d%d",&x,&n);
printf("\nvalue = %d",power(x,n));
break;
}
}while(op!=3);
}
No comments:
Post a Comment