0% found this document useful (0 votes)
2 views16 pages

Data Struture Lab File Unit 1

The document contains a series of C programming exercises focused on various data structures and algorithms, specifically stack operations, recursion, and mathematical computations. Each section includes a code snippet along with expected output for tasks such as implementing a stack, calculating factorials, generating Fibonacci series, and more. The exercises are designed for Btech 1st year students to practice and understand fundamental programming concepts.

Uploaded by

mralmanjo
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)
2 views16 pages

Data Struture Lab File Unit 1

The document contains a series of C programming exercises focused on various data structures and algorithms, specifically stack operations, recursion, and mathematical computations. Each section includes a code snippet along with expected output for tasks such as implementing a stack, calculating factorials, generating Fibonacci series, and more. The exercises are designed for Btech 1st year students to practice and understand fundamental programming concepts.

Uploaded by

mralmanjo
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

DATA STRUTURE LAB FILE

Btech 1st year 2nd sem


Stack data structure
Unit 1
1. Write a C program to perform All Stack Operation.
Such as PUSH, POP, PEEK, isEmpty, isFull.

#include <stdio.h>
#define MAX 5 // maximum size of stack
int stack[MAX];
int top = -1;
// Function to check if stack is full
int isFull() {
if (top == MAX - 1)
printf("Stack is Full \n”);
else
printf("Stack is not Full\n”);
}
// Function to check if stack is empty
int isEmpty() {
if (top == -1)
printf("Stack is Empty \n”);
else
printf("Stack is not Empty \n”);
}

// Function to push element into stack


void push(int value) {
if (top==MAX-1)
printf("Stack Overflow! Cannot push %d\n", value);
else
{
top++;
stack[top] = value;
printf("%d pushed into stack\n", value);
}
}

// Function to pop element from stack


void pop() {
if (top==-1)
printf("Stack Underflow! Cannot pop\n");
else
{
printf("%d popped from stack\n", stack[top]);
top--;
}
}

// Function to peek top element of stack


void peek() {
if (top==-1)
printf("Stack is empty! No top element\n");
else
printf("Top element is %d\n", stack[top]);
}

// Main function
int main() {
push(10);
push(20);
push(30);
pop();
pop();
peek();
isEmpty();
isFull();
return 0;
}

OUTPUT : 10 pushed into stack


20 pushed into stack
30 pushed into stack
30 popped from stack
20 popped from stack
Top element is 10
Stack is not Empty
Stack is not Full
2. Write a C program to perform Display operation on
stack.

#include <stdio.h>
#define MAX 5

int stack[MAX];
int top = -1;

void display() {
int i;
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Stack elements are:\n");
for (i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}

int main() {
// Sample stack values
stack[0] = 10;
stack[1] = 20;
stack[2] = 30;
top = 2;

display();
return 0;
}

OUTPUT : Stack elements are:


30
20
10
3. Write a C program to implement Tower of Hanoi using
Recursion Function.
#include <stdio.h>
// Function to solve Tower of Hanoi
void towerOfHanoi(int n, char source, char auxiliary, char destination) {
// Base case
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
// Move n-1 disks from source to auxiliary
towerOfHanoi(n - 1, source, destination, auxiliary);
// Move nth disk from source to destination
printf("Move disk %d from %c to %c\n", n, source, destination);
// Move n-1 disks from auxiliary to destination
towerOfHanoi(n - 1, auxiliary, source, destination);
}
int main() {
int n;
printf("Enter number of disks: ");
scanf("%d", &n);
// A = Source, B = Auxiliary, C = Destination
towerOfHanoi(n, 'A', 'B', 'C');
return 0;
}

OUTPUT : Enter number of disks: 3


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
4. Write a C program to perform Factorial using
Recursion Operation .
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %d", factorial(n));
return 0;
}

OUTPUT : Enter a number: 3


Factorial = 6
5. Write a C program to perform Fibonacci Series using
Recursion Operation .
#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, i;
printf("Enter number of terms: ");
scanf("%d", &n);

for (i = 0; i < n; i++)


printf("%d ", fibonacci(i));
return 0;
}

OUTPUT : Enter number of terms: 10


0 1 1 2 3 5 8 13 21 34
6. Write a C program to perform Sum of First N Natural
Numbers using Recursion Operation .

#include <stdio.h>
int sum(int n) {
if (n == 0)
return 0;
else
return n + sum(n - 1);
}

int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);

printf("Sum = %d", sum(n));


return 0;
}

OUTPUT : Enter n: 5
Sum = 15
7. Write a C program to perform Count Digits of a
Number Using Recursion Operation .

#include <stdio.h>
int countDigits(int n) {
if (n == 0)
return 0;
else
return 1 + countDigits(n / 10);
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

printf("Number of digits = %d", countDigits(n));


return 0;
}

OUTPUT : Enter a number: 12345


Number of digits = 5
8. Write a C program to perform Power of a Number
Using Recursion Operation .
#include <stdio.h>
int power(int x, int n) {
if (n == 0)
return 1;
else
return x * power(x, n - 1);
}

int main() {
int x, n;
printf("Enter base and exponent: ");
scanf("%d %d", &x, &n);

printf("Result = %d", power(x, n));


return 0;
}

OUTPUT : Enter base and exponent: 2 2


Result = 4
9. Write a C program to perform Reverse of a Number
Using Recursion Operation .

#include <stdio.h>

int reverse(int n, int rev) {


if (n == 0)
return rev;
else
return reverse(n / 10, rev * 10 + (n % 10));
}

int main() {
int n, rev;
printf("Enter a number: ");
scanf("%d", &n);

rev = reverse(n, 0);

if (n == rev)
printf("Palindrome number");
else
printf("Not a palindrome number");

return 0;
}

OUTPUT : Enter a number: 121


Palindrome number
10. Write a C program to perform GCD of Two Numbers
(Euclidean Algorithm) Using Recursion Operation .

#include <stdio.h>

int gcd(int a, int b) {


if (b == 0)
return a;
else
return gcd(b, a % b);
}

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

printf("GCD = %d", gcd(a, b));


return 0;
}

OUTPUT : Enter two numbers: 5 10


GCD = 5
11. Write a C program to perform Print Numbers from
1 to N Using Recursion Operation .

#include <stdio.h>

void printNumbers(int n) {
if (n == 0)
return;
printNumbers(n - 1);
printf("%d ", n);
}

int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printNumbers(n);
return 0;
}

OUTPUT : Enter n: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
12. Write a C program to perform Print Numbers from N
to 1 Using Recursion Operation .

#include <stdio.h>

void printReverse(int n) {
if (n == 0)
return;
printf("%d ", n);
printReverse(n - 1);
}

int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printReverse(n);
return 0;
}

OUTPUT : Enter n: 20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

You might also like