0% found this document useful (0 votes)
3 views6 pages

Ds Lab Programs

The document contains three C programs demonstrating different functionalities: Program 1 prints the elements of an array recursively, Program 2 solves the Towers of Hanoi problem using recursion, and Program 5 implements a stack with operations such as push, pop, peek, and display. Each program includes sample outputs showcasing their respective functionalities. The programs serve as educational examples for understanding recursion and data structures in C.

Uploaded by

pavanbehara57
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Ds Lab Programs

The document contains three C programs demonstrating different functionalities: Program 1 prints the elements of an array recursively, Program 2 solves the Towers of Hanoi problem using recursion, and Program 5 implements a stack with operations such as push, pop, peek, and display. Each program includes sample outputs showcasing their respective functionalities. The programs serve as educational examples for understanding recursion and data structures in C.

Uploaded by

pavanbehara57
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM-1

#include<stdio.h>

Void printArray (int arr[],int size,int index)

if(index==size){

return;

printf("%d",arr[index]);

printArray(arr,size,index+1);

int main(){

int arr[]={10,20,30,40,50};

int size =sizeof(arr)/sizeof(arr[0]);

printf("Array elements are:");

printArray(arr,size,0);

return 0;

OUT PUT

Array elements are:10,20,30,40,50

Program -2
#include <stdio.h>

void towersOfHanoi(int n, char source, char auxiliary, char destination) {

if (n == 1) {

printf("Move disk 1 from %c to %c\n", source, destination);

return;

towersOfHanoi(n - 1, source, destination, auxiliary);

printf("Move disk %d from %c to %c\n", n, source, destination);

towersOfHanoi(n - 1, auxiliary, source, destination);

int main() {
int n;

printf("Enter the number of disks: ");

scanf("%d", &n);

printf("Steps to solve the Towers of Hanoi problem:\n");

towersOfHanoi(n, 'A', 'B', 'C');

return 0;

Out put
Enter the number of disks: 5

Steps to solve the Towers of Hanoi problem:

Move disk 1 from A to C

Move disk 2 from A to B

Move disk 1 from C to B

Move disk 3 from A to C

Move disk 1 from B to A

Move disk 2 from B to C

Move disk 1 from A to C

Move disk 4 from A to B

Move disk 1 from C to B

Move disk 2 from C to A

Move disk 1 from B to A

Move disk 3 from C to B

Move disk 1 from A to C

Move disk 2 from A to B

Move disk 1 from C to B

Move disk 5 from A to C

Move disk 1 from B to A

Move disk 2 from B to C

Move disk 1 from A to C

Move disk 3 from B to A

Move disk 1 from C to B


Move disk 2 from C to A

Move disk 1 from B to A

Move disk 4 from B to C

Move disk 1 from A to C

Move disk 2 from A to B

Move disk 1 from C to B

Move disk 3 from A to C

Move disk 1 from B to A

Move disk 2 from B to C

Move disk 1 from A to C

Program -5
#include <stdio.h>

#define MAX 100

typedef struct {

int items[MAX];

int top;

} Stack;

void initializeStack(Stack *s) {

s->top = -1;

int isEmpty(Stack *s) {

return s->top == -1;

int isFull(Stack *s) {

return s->top == MAX - 1;

void push(Stack *s, int value) {

if (isFull(s)) {

printf("Stack is full!\n");

return;

}
s->items[++s->top] = value;

printf("Pushed: %d\n", value);

int pop(Stack *s) {

if (isEmpty(s)) {

printf("Stack is empty!\n");

return -1;

int value = s->items[s->top--];

printf("Popped: %d\n", value);

return value;

int peek(Stack *s) {

if (isEmpty(s)) {

printf("Stack is empty!\n");

return -1;

return s->items[s->top];

void display(Stack *s) {

if (isEmpty(s)) {

printf("Stack is empty!\n");

return;

printf("Stack elements: ");

for (int i = 0; i <= s->top; i++) {

printf("%d ", s->items[i]);

}
printf("\n");

int main() {

Stack s;

initializeStack(&s);

int choice, value;

do {

printf("\nStack Operations:\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Peek\n");

printf("4. Display\n");

printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

scanf("%d", &value);

push(&s, value);

break;

case 2:

pop(&s);

break;

case 3:

value = peek(&s);

if (value != -1) {

printf("Top element: %d\n", value);

break;

case 4:
display(&s);

break;

case 5:

printf("Exiting...\n");

break;

default:

printf("Invalid choice!\n");

} while (choice != 5);

return 0;

Output

Stack Operations:

1. Push

2. Pop

3. Peek

4. Display

5. Exit

Enter your choice: 5

Exiting...

You might also like