Implementation of Recursion
#include<iostream>//headerfile
using namespace std;//for std
#define size 5//fixed size
struct Stack{ //to make stack
int arr[size];
int top = -1;
bool isEmpty()//to check stack is this empty
{
return (top < 0);
}
bool isFull()//to check stack is this full
{
return (top == size - 1);
}
void push(int n) //to push the value and store it into the stack
{
if (isFull())
{
cout << "Stack is Full" << endl;
}
else
{
arr[++top] = n;
}
}
int pop()//to remove the value from stack
{
if (isEmpty())
{
cout << "There is nothing to show" << endl;
}
else
{
int a = arr[top--];
return a;
}
}
int peek()//to remove or check the top of the stack
{
if (isEmpty())
{
cout << "There is noting to show" << endl;
}
else
{
int a = arr[top];
return a;
}
}
int sum(int n)//Recusrive function to make sum
{
if (n <= 1)
{
return 1;
}
else
{
return n + sum(n - 1);
}
}
int fact(int n)//Recusrive function to take factorial
{
if (n <= 1)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
};
int main()
{
Stack s1;
int x = 10;
s1.push(s1.sum(x));
s1.push(s1.fact(x));
int a = s1.pop();
cout <<"Factorial is:"<< a << endl;
a = s1.pop();
cout << "Sum of the series is:" << a << endl;
getchar();
getchar();
return 0;
}
Comments
Post a Comment