0% found this document useful (0 votes)
2 views

PROGRAMS

The document contains three separate C programs: one for calculating the Fibonacci number of a given integer using recursion, another for implementing a queue using arrays with functions for enqueue, dequeue, and display, and a third for implementing a stack using arrays with functions for push, pop, and print. Each program includes a main function that allows user interaction through a menu-driven interface. The code demonstrates basic data structures and their operations in C.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PROGRAMS

The document contains three separate C programs: one for calculating the Fibonacci number of a given integer using recursion, another for implementing a queue using arrays with functions for enqueue, dequeue, and display, and a third for implementing a stack using arrays with functions for push, pop, and print. Each program includes a main function that allows user interaction through a menu-driven interface. The code demonstrates basic data structures and their operations in C.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q.

Fibonacci of nth number

#include<stdio.h>
int Fibonacci(int n){
if( n == 0){
return 0;
}
else if(n == 1 ){
return 1;
}
else{
return Fibonacci(n-1) + Fibonacci(n - 2);
}
}
int main(){
int n;
scanf("%d", &n);
printf("%d", Fibonacci(n));
return 0;
}

Q.queue using arrays

#include <stdio.h>
# define max 100
int queue[max];
int front = -1, rear = -1;
void enqueue(int data){
if((rear + 1) % max == front){
printf("overflow\n");
return ;
}
if(front == -1){
front = rear = 0;
}
else{
rear = (rear + 1) % max ;

}
queue[rear] = data;
}
int dequeue(){
if(front == -1){
printf("underflow\n");
return -1;
}
int data = queue[front];
if(front == rear){
front = rear = -1;
}
else{
front = (front + 1) % max ;
}
return data;
}
void display()
{
// If the queue is empty, print a message and return
if (front == -1) {
printf("Queue is empty\n");
return;
}
// Print the elements in the queue
printf("Queue elements: ");
int i = front;
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % max;
}
// Print the last element
printf("%d\n", queue[rear]);
}
int main(){
int ch, data;
while(1){
printf("1.enqueue\n2.dequeue\n3.print queue\n4.exit\nenter your choice :
");
scanf("%d", &ch);
switch(ch){
case 1 :
printf("enter the element :");
scanf("%d", &data);
enqueue(data);
break;
case 2 :
data = dequeue();
if(data != -1){
printf("deueued element :%d\n", data);
}
break;
case 3 :
display();
break;
case 4 :
return 0;
break;
default:
printf("invalid retry !");
}
}
}

Q.stacks using arrays

# include <stdio.h>
#define max 100
int stack[100];
int top = -1;
void push(int a) {
if (top == max - 1) {
printf("\nStack overflow\n");
} else {
top = top + 1;
stack[top] = a;
}
}
int pop(){
int data = 0;
if(top == -1){
printf("\n underflow \n");
return -1;
}
else{
data = stack[top];
top = top - 1;
return data;
}
}
void print_stack(){
if(top == -1){
printf("stack is empty\n ");
return;
}
else {
printf("stack elements:");
for(int i = top; i >= 0; i--){
printf("\n%d\n", stack[i]);
}
}
}
int main(){
int ch , data;
while(1){
printf("1.push\n2.pop\n3.print stack\n4.exit\nenter your choice : ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("enter the data :");
scanf("%d", &data);
push(data);
break;
case 2:
data = pop();
if(data != -1){
printf("poped element : %d\n", data);
}
break;
case 3 :
print_stack();
break;
default:
return 0;
}
}
return 0;
}

You might also like