DS Lab Programs
1. Program to perform arithmetic operations using switch-case statement.
#include<stdio.h>
int main( )
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n3.Multiplication\n 4.Division\n
5.Modulus\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1: printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2:printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3:printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4:printf("Division of Two Numbers is %d : ",a/b);
break;
case 5:printf("Mod of Two Numbers is %d : ",a%b);
break;
default:printf(" Enter Your Correct Choice.");
break;
}
return 0;
}
2. Program to accept a character & display the entered character is an alphabet,
a number or a symbol.
#include <stdio.h>
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Alphabet check */
if((ch>= 'a' && ch<= 'z') || (ch>= 'A' && ch<= 'Z'))
{
printf("'%c' is alphabet.", ch);
}
else if(ch>= '0' &&ch<= '9')
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}
return 0;
}
3. Program to read marks of a student and display the result with grades.
#include<stdio.h>
int main( )
{
int i;
float mark, sum=0, avg;
printf("Enter Marks obtained in 5 Subjects: ");
for(i=0; i<5; i++)
{
scanf("%f", &mark);
sum = sum+mark;
}
avg = sum/5;
printf("\nGrade = ");
if(avg>=91 && avg<=100)
printf("A+");
else if(avg>=81 && avg<91)
printf("A");
else if(avg>=71 && avg<81)
printf("B");
else if(avg>=61 && avg<71)
printf("C");
else if(avg>=51 && avg<61)
printf("D");
else if(avg>=41 && avg<51)
printf("E");
else
printf("F");
return 0;
}
4. Program to generate the following pattern:
*
**
***
****
#include <stdio.h>
int main()
{
int i,j;
for(i=0;i<=4;i++)
{
for(j=0;j<i;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
5. Program to check whether the given number is palindrome or not.
#include <stdio.h>
int main( )
{
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
//reversed integer is stored in reversed variable
while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
6. Program to find smallest and largest element in an array.
#include <stdio.h>
int main()
{
int a[100], n, i, small, large;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 0; i< n; i++)
{
printf("Enter element %d : ", i + 1);
scanf("%d", &a[i]);
}
small = a[0];
large = a[0];
for (i = 1; i< n; i++)
{
if (a[i] < small)
{
small = a[i];
}
if (a[i] > large)
{
large = a[i];
}
}
printf("\nLargest element is : %d", large);
printf("\nSmallest element is : %d", small);
return 0;
}
7. Program to display number of even and odd numbers in a given array.
#include<stdio.h>
int main()
{
int Size, i, a[20];
int Even_Count = 0, Odd_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i< Size; i++)
{
scanf("%d", &a[i]);
}
for(i = 0; i< Size; i ++)
{
if(a[i] % 2 == 0)
{
Even_Count++;
}
else
{
Odd_Count++;
}
}
printf("\n Total Number of Even Numbers = %d ", Even_Count);
printf("\n Total Number of Odd Numbers = %d ", Odd_Count);
return 0;
}
8. Program to demonstrate the use of string functions: length, concat and
reverse.
#include <stdio.h>
#include <string.h>
int main()
{
char str[40],s1[20],s2[20]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
printf (" \n Reverse of a string: %s ", strrev(str));
printf (" \n\n Length of a string: %d ", strlen(str));
printf (" \n\n Enter 2 strings to concat: ");
scanf ("%s%s", s1,s2);
printf (" \n Concatination of 2 strings: %s ", strcat(s1,s2));
return 0;
}
9. Program to display Fibonacci numbers using function.
#include<stdio.h>
void printFibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main( )
{
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2); //n-2 because 2 numbers are already printed
return 0;
}
10. Program to compute factorial of a number using recursive function.
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main( )
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
}
11. Program to find sum of matrices using 2D Array.
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of columns: ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("Enter element a[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element b [%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i< r; ++i)
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i< r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
}
printf("\n\n");
}
return 0;
}
12. Program to implement tower of Hanoi using recursive function.
#include<stdio.h>
#include<conio.h>
void tower(int n,int source,int temp,int destination)
{
if(n==1)
{
printf("Move disk %d from %c to %c\n",n,source,destination);
return;
}
tower(n-1,source,destination,temp);
printf("Move disk %d from %c to %c\n",n,source,destination);
tower(n-1,temp,source,destination);
}
void main()
{
int m,n;
printf("Enter the no of disks\n");
scanf("%d",&n);
tower(n,'S','T','D');
}
13. Program to sort the given list using selection sort.
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
14. Program to sort the given list using bubble sort.
/* Bubble sort code */
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
15. Program to search an element using linear search technique.
#include <stdio.h>
int main()
{
int array[100], search, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (array[i] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
16. Program to search an element using binary search technique.
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last)
{
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
17. Program to demonstrate dynamic memory allocation.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Addresses of previously allocated memory:\n");
for(i = 0; i < n1; ++i)
printf("%pc\n",ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
// rellocating the memory
ptr = realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated memory:\n");
for(i = 0; i < n2; ++i)
printf("%pc\n", ptr + i);
free(ptr);
return 0;
}
18. Program to implement stack.
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
while(choice!=4)
{
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf("\n\t EXIT POINT ");
break;
default:printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\t STACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=0; i<=top; i++)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
19. Program to implement simple queue.
#include<stdio.h>
#include<stdlib.h> //for exit(o) which is in stdlib.h file
int q[10], size, front=0, rear=-1;
void Qinsert( )
{
int item;
if(rear==size-1)
{
printf("Queue is full\n");
return;
}
printf("Enter the item to insert in Queue\n");
scanf("%d", &item);
rear=rear+1;
q[rear]=item;
}
void Qdelete( )
{
if(front>rear)
{
printf("Queue is empty\n");
return;
}
printf("deleted item from Queue is %d\n",q[front]);
front=front+1;
if(front>rear)
{
rear=-1;
front=0;
}
}
void Qdisplay()
{
int i;
if(front>rear)
{
printf("Queue is empty\n");
return;
}
printf("Items in Queue are\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
int main()
{
int ch;
printf("Enter the size of Queue\n");
scanf("%d", &size);
while(1)
{
printf("\nThe operations in Queue are\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Choose the option\n");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("You selected option is insert\n");
Qinsert();
break;
case 2: printf("You selected option is delete\n");
Qdelete();
break;
case 3: printf("You selected option is display\n");
Qdisplay();
break;
case 4: exit(0);
default: printf("You selected option wrong\n");
}
}
}
20. Program to implement linear linked list.
a. Insert, Delete and Display.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void insert( );
void display( );
void delete( );
void main()
{
int ch;
while(1)
{
printf("\n1:insert\n2:delete\n3:display\n4:exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("Please enter the correct choice!\n");
}
}
}
struct node* getnode()
{
struct node *x;
x=(struct node*)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory");
exit(0);
}
return x;
}
void insert()
{
struct node *ptr;
int item;
ptr=getnode();
printf("\nenter the value\n");
scanf("%d",&item);
ptr->data=item;
ptr->link=head;
head=ptr;
printf("Node inserted %d\n",ptr->data);
}
void delete()
{
struct node *ptr;
if(head==NULL)
{
printf("List is empty");
}
else
{
ptr=head;
printf("\nNode %d deleted from the beginning\n",ptr->data);
head=ptr->link;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==NULL)
{
printf("Nothing to print list is empty");
}
else
{
printf("\nContents of singly linked list\n");
while(ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr=ptr->link;
}
}
}
b. Insert, Search and Display.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*head;
void insert();
void display();
void search();
void main()
{
int ch;
while(1)
{
printf("\n1:insert\n2:search\n3:display\n4:exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:search();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("Please enter the correct choice!\n");
}
}
}
struct node* getnode()
{
struct node *x;
x=(struct node*)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory");
exit(0);
}
return x;
}
void insert()
{
struct node *ptr;
int item;
ptr=getnode();
printf("\nenter the value\n");
scanf("%d",&item);
ptr->data=item;
ptr->link=head;
head=ptr;
printf("Node inserted %d\n",ptr->data);
}
void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr=head;
if(ptr==NULL)
{
printf("List is empty");
}
else
{
printf("enter the item which you want to search\n");
scanf("%d",&item);
while(ptr!=NULL)
{
if(ptr->data==item)
{
printf("item found at location %d",i+1);
flag=0;
break;
}
i++;
ptr=ptr->link;
}
if(flag==1)
{
printf("\nitem not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==NULL)
{
printf("Nothing to print list is empty");
}
else
{
printf("\nContents of singly linked list\n");
while(ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr=ptr->link;
}
}
}