Professional Documents
Culture Documents
Untitled
Untitled
Untitled
1. Recursion Problem
Code:
#include <stdio.h>
int factorial(int n); // function declaration
int main()
{
int n, fact;
printf("Enter a positive integer: ");
scanf("%d", &n); //Taking the number as input from user
fact = factorial(n); // calling function
printf("Factorial of %d = %d", n, fact); // printing output
return 0;
}
2. Stack – Problem
Code:
#include <stdio.h>
main()
{
int ch;
do
{
case 2:
Pop(); // calling pop function
break;
case 3:
Top(); // calling top function
break;
case 4:
printf("\n Exit"); // Exit the loop of giving options everytime
break;
default:
printf("\n Enter a valid choice plz"); // Incase if user inputs
value not in option
break;
}
} while(ch!=4); // loop options unless the user gives exit option
top++; //incrementing 1
int x;
if(top==4) // checking if the stack is full
printf("Stack overflow");
else
{
printf("\n Enter the element that you want to add");
scanf("%d",&x); // input into x
stack[top]= x; // x value given to stack.
}
}
void Top() //function definition
{
if (top == -1) // checking if there are no elements in the stack
{
printf("Empty Stack") ; // print Empty stack
}
else
printf("\n ToS is %d",stack[top]); //else print the top most element
}
OUTPUT