Mca 1ST Sem Programs & Outputs 2022
Mca 1ST Sem Programs & Outputs 2022
ARRAY OPERATIONS
1. PROGRAM:-
#include<stdio.h>
#include<conio.h>
/* Global variables declaration */
int a[4], n, elem, i, pos;
/*Function Prototype and Definitions*/
void create()
{
printf("\nEnter the size of the array elements: ");
scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
}
void display()
{
int i;
printf("\nThe array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t", a[i]);
}
}
void insert()
{
printf("\nEnter the position for the new element: ");
scanf("%d", &pos);
printf("\nEnter the element to be inserted: ");
scanf("%d", &elem);
for(i=n-1; i>=pos-1; i--)
{
a[i+1] = a[i];
}
a[pos-1] = elem;
n = n+1;
}
void del()
{
1
printf("\nEnter the position of the element to be deleted: ");
scanf("%d", &pos);
elem = a[pos-1];
for(i=pos-1; i<n-1; i++)
{
a[i] = a[i+1];
}
n = n-1;
printf("\nThe deleted element is = %d", elem);
}
void main()
{
int ch;
clrscr();
do{
printf("\n\n--------Menu----------- \n");
printf("1.Create\n 2.Display\n 3.Insert\n 4.Delete\n 5.Exit\n");
printf(" ");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break;
case 4: del();
break;
case 5: exit(0);
break;
default: printf("\nInvalid choice:\n");
break;
}
}while(ch!=5);
getch();
}
2
SAMPLE OUTPUT:-
------------- Menu--------------
1.Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 1
Enter the size of the array elements: 5 Enter the elements for the array:
10 20 30 40 50
------------- Menu--------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 2
The array elements are:
10 20 30 40 50
------------- Menu--------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 3
Enter the position for the new element: 2
Enter the element to be inserted: 90
------------- Menu---------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 2
The array elements are:
10 20 90 30 40 50
------------- Menu----------------
1. Create 2. Display 3.Insert 4.Delete 5.Exit
Enter your choice: 4
Enter the position of the element to be deleted: 5
The deleted element is = 50
------------- Menu----------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 2
The array elements are:
10 20 90 30 40
------------- Menu----------------
1. Create 2.Display 3.Insert 4.Delete 5.Exit
Enter your choice: 5
3
2. STACK OPERATIONS USING ARRAY
2. PROGRAM:-
#include<stdio.h>
#include<conio.h>
#define MAX 4
int stack[MAX], item;
int ch, top = -1, count = 0, status = 0;
/*PUSH FUNCTION*/
void push(int stack[], int item)
{
if (top == (MAX-1))
printf("\n\nStack is Overflow");
else
{
stack[++top] = item;
status++;
}
}
/*POP FUNCTION*/
int pop(int stack[])
{
int ret;
if(top == -1)
printf("\n\nStack is Underflow");
else
{
ret = stack[top--];
status--;
printf("\nPopped element is %d", ret);
}
return ret;
}
/* FUNCTION TO CHECK STACK IS PALINDROME OR NOT */
void palindrome(int stack[])
{
int i, temp;
temp = status;
4
for(i=0; i<temp; i++)
{
if(stack[i] == pop(stack))
count++;
}
if(temp==count)
printf("\nStack contents are Palindrome");
else
printf("\nStack contents are not palindrome");
}
/*FUNCTION TO DISPLAY STACK*/
void display(int stack[])
{
int i;
printf("\nThe stack contents are:");
if(top == -1)
printf("\nStack is Empty");
else
{
for(i=top; i>=0; i--)
printf("\n ------\n| %d |", stack[i]);
printf("\n");
}
}
/*MAIN PROGRAM*/
void main()
{
clrscr();
do{
printf("\n\n----MAIN MENU \n");
printf("\n1. PUSH (Insert) in the Stack");
printf("\n2. POP (Delete) from the Stack");
printf("\n3. PALINDROME check using Stack");
printf("\n4. Exit (End the Execution)");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch){
5
case 1: printf("\nEnter a element to be pushed: ");
scanf("%d", &item);
push(stack, item);
display(stack);
break;
case 2: item=pop(stack);
display(stack);
break;
case 3:palindrome(stack);
break;
case 4:exit(0);
break;
default:
printf("\nEND OF EXECUTION");
}//end switch
}
while (ch != 4);
getch();
}
6
SAMPLE OUTPUT:-
----MAIN MENU----
1.PUSH (Insert) in the Stack
2.POP (Delete) from the Stack
3.PALINDROME check using Stack
4.Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
|1|
----MAIN MENU----
1.PUSH (Insert) in the Stack
2.POP (Delete) from the Stack
3.PALINDROME check using Stack
4.Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 2
The stack contents are:
|2|
|1|
7
Enter Your Choice: 2
Popped element is 1
The stack contents are:
|2|
|2|
|1|
(-------- AFTER THE 4 TIMES POP OPERATION )
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 2
Stack is Underflow
The stack contents are:
Stack is Empty
(-------- CHECKING FOR PALINDROME OR NOT USING STACK )
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 2
The stack contents are:
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
8
4. Exit (End the Execution) Enter Your Choice: 1
Enter a element to be pushed: 1
The stack contents are:
|1|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 3
Stack contents are is Palindrome
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 1
The stack contents are:
|1|
(AFTER 3 TIMES PUSH)
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 1
Enter an element to be pushed: 3
The stack contents are:
|3|
|2|
|1|
----MAIN MENU----
1. PUSH (Insert) in the Stack
2. POP (Delete) from the Stack
3. PALINDROME check using Stack
4. Exit (End the Execution) Enter Your Choice: 3
Stack contents are not Palindrome
9
3. INFIX TO POSTFIX CONVERSION
3. PROGRAM:-
#include<stdio.h>
#include<string.h>
#include<conio.h>
int F(char symbol)
{
switch(symbol)
{
case '+' :
case '-': return 2;
case '*':
case '/': return 4;
case '^':
case '$': return 5;
case '(': return 0;
case '#': return -1;
default: return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-': return 1;
case '*':
case '/': return 3;
case '^':
case '$': return 6;
case '(': return 9;
case ')': return 0;
default: return 7;
}
}
void infix_postfix(char infix[], char postfix[])
{
10
int top, j, i;
char s[30], symbol;top = -1;
s[++top] = '#';
j = 0;
for(i=0; i < strlen(infix); i++)
{
symbol = infix[i];
while(F(s[top]) > G(symbol))
{
postfix[j] = s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++] = s[top--];
}
postfix[j] = '\0';
}
void main()
{
char infix[20], postfix[20];
clrscr();
printf("\nEnter a valid infix expression\n");
flushall();
gets(infix);
infix_postfix(infix,postfix);
printf("\nThe infix expression is:\n");
printf ("%s",infix);
printf("\nThe postfix expression is:\n");
printf ("%s",postfix);
getch();
}
11
SAMPLE OUTPUT:-
Enter a valid infix expression (a+(b-c)*d)
The infix expression is: (a+(b-c)*d)
The postfix expression is: abc-d*+
12
4. CIRCULAR QUEUE
4. PROGRAM:-
#include<stdio.h>
#include<conio.h>
#define MAX 4
int ch, front = 0, rear = -1, count=0;
char q[MAX], item;
void insert()
{
if(count == MAX)
printf("\nQueue is Full");
else
{
rear = (rear + 1) % MAX;
q[rear]=item;
count++;
}
}
void del()
{
if(count == 0)
printf("\nQueue is Empty");
else
{
if(front > rear && rear==MAX-1)
{
front=0;
rear=-1;
count=0;
}
else
{
item=q[front];
printf("\nDeleted item is: %c",item);
front = (front + 1) % MAX;
count--;
13
}
}
}
void display()
{
int i;
if(count == 0)
printf("\nQueue is Empty");
else
{
printf("\nContents of Queue is:\n");
i=front;
while(i!=rear)
{
printf("%c\t", q[i]);
i=(i+1)%MAX;
}
printf("%c\t",q[rear]);
}
}
void main()
{
clrscr();
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
flushall();
switch(ch)
{
case 1: printf("\nEnter the character / item to be inserted: ");
scanf("%c", &item);
insert();
break;
case 2: del();
break;
14
case 3: display();
break;
case 4: exit(0);
break;
}
}
while(ch!=4);
getch();
}
15
SAMPLE OUTPUT:
1. Insert 2. Delete 3. Display 4. Exit Enter the choice: 1
Enter the character / item to be inserted: A
Queue is Full
16
5. SINGLE LINKED LIST
5. PROGRAM:-
#include<stdio.h>
#include<conio.h>
int MAX=4, count;
struct student
{
char usn[10]; char name[30]; char branch[5]; int sem;
char phno[10];
struct student *next; //Self referential pointer.
};
typedef struct student NODE;
int countnodes(NODE *head)
{
NODE *p;
count=0; p=head;
while(p!=NULL)
{
p=p->next; count++;
}
return count;
}
17
{
p=head;
printf("\n----STUDENT DATA \n");
printf("\nUSN\tNAME\t\tBRANCH\tSEM\tPh.NO.");
while(p!=NULL)
{
printf("\n%s\t%s\t\t%s\t%d\t%s", p->usn, p->name, p->branch, p->sem, p-
>phno);
p = p->next; //Go to next node...
}
printf("\nThe no. of nodes in list is: %d",countnodes(head));
}
return head;
}
18
p=head;
if(countnodes(head) == MAX)
printf("\nList is Full(QUEUE)!!");
else
{
if(head == NULL)
{
newnode=getnode(head);
head=newnode; //set first node to be head
}
else
{
newnode=getnode(head);
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
}
}
return head;
}
19
}
20
head=display(head);
}
while(ch!=3);
return head;
}
21
void main()
{
int ch, i, n; NODE *head;
head=NULL;
clrscr();
printf("\n*----------Studednt Database-------------*");
do
{
printf("\n 1.Create\t 2.Display\t 3.Insert\t 4.Delete\t 5.Stack\t 6.Queue\t 7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nHow many student data you want to create: ");
scanf("%d", &n);
for(i=0;i<n;i++)
head=create(head);//Call to Create NODE...
break;
case 2: head=display(head);
break;
case 3: head=insert(head); //Call to Insert...
break;
case 4: head=del(head); //Call to delete
break;
case 5: head=stack(head);
break;
case 6: head=queue(head);
break;
case 7: exit(); //Exit...
}
}
while(ch!=7);
getch();
}
22
SAMPLE OUTPUT:-
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7.
Exit Enter your choice: 1
How many student data you want to create: 2 Enter USN, Name, Branch, Sem,
Ph.No
1kt12cs001 kumar mca 1 9900099000
Enter USN, Name, Branch, Sem, Ph.No
1kt12is002 ravi msc 1 9900099111
1. Create 2. Display 3. Insert 4. Delete 5. Stack 6.Queue 7.
Exit Enter your choice: 2
----STUDENT DATA----
USN NAME BRANCH SEM Ph.NO.
1kt12is002 ravi msc 1 9900099111
1kt12cs001 kumar mca 1 9900099000
23
6. BINARY SEARCH TREE
6. PROGRAM:-
#include <stdio.h>
#include <stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;
NODE *node;
NODE* createtree(NODE *node, int data)
{
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data < (node->data))
{
node->left = createtree(node->left, data);
}
else if (data > node->data)
{
node -> right = createtree(node->right, data);
}
return node;
}
24
{
node->right=search(node->right, data);
}
else
printf("\nElement found is: %d", node->data);
return node;
}
void inorder(NODE *node)
{
if(node != NULL)
{
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
}
}
void preorder(NODE *node)
{
if(node != NULL)
{
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
}
}
void postorder(NODE *node)
{
if(node != NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
}
}
NODE* findMin(NODE *node)
{
if(node==NULL)
{
return NULL;
}
if(node->left)
return findMin(node->left);
else
25
return node;
}
26
void main()
{
int data, ch, i, n; NODE *root=NULL;
clrscr();
while (1)
{
printf("\n1.Insertion in Binary Search Tree");
printf("\n2.Search Element in Binary Search Tree");
printf("\n3.Delete Element in Binary Search Tree");
printf("\n4.Inorder\n5.Preorder\n6.Postorder\n7.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1: printf("\nEnter N value: " );
scanf("%d", &n);
printf("\nEnter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)\n");
for(i=0; i<n; i++)
{
scanf("%d", &data);
root=createtree(root, data);
}
break;
case 2: printf("\nEnter the element to search: ");
scanf("%d", &data);
root=search(root, data); break;
case 3: printf("\nEnter the element to delete: ");
scanf("%d", &data);
root=del(root, data); break;
case 4: printf("\nInorder Traversal: \n");
inorder(root); break;
case 5: printf("\nPreorder Traversal: \n");
preorder(root); break;
case 6: printf("\nPostorder Traversal: \n");
postorder(root); break;
case 7: exit(0);
default:printf("\nWrong option");
break;
}
}
}
27
SAMPLE OUTPUT:-
1. Insertion in Binary Search Tree
2. Search Element in Binary Search Tree
3. Delete Element in Binary Search Tree
4. Inorder
5. Preorder
6. Postorder
7. Exit
Enter your choice: 1
Enter N value: 12
Enter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)
6 9 5 2 8 15 24 14 78 5 2
1. Insertion in Binary Search Tree
2. Search Element in Binary Search Tree
3. Delete Element in Binary Search Tree
4. Inorder
5. Preorder
6. Postorder
7. Exit
Enter your choice: 4
Inorder Traversal:
2 5 6 7 8 9 14 15 24
1. Insertion in Binary Search Tree
2. Search Element in Binary Search Tree
3. Delete Element in Binary Search Tree
4. Inorder
5. Preorder
6. Postorder
7. Exit
Enter your choice: 5
Preorder Traversal:
6 5 2 9 8 7 15 14 24
1.Insertion in Binary Search Tree
2.Search Element in Binary Search Tree
3.Delete Element in Binary Search Tree
4.Inorder
5.Preorder
6.Postorder
7.Exit
Enter your choice: 6
Postorder Traversal:
2 5 7 8 14 24 15 9 6
28
7. SEARCHING TECHNIQUES
7. PROGRAM:-
7A. Linear search:-
#include<stdio.h>
void main()
{
int a[20],i,x,n;
printf("How many elements?"); scanf("%d",&n);
for(i=0;i<n;++i) if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i); else
printf("Element not found");
getch();
}
Output:-
How many elements? 6 Enter array elements: 5
8
29
7B.Binary search:-
#include <stdio.h>
#include<conio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
Output:-
Enter a element u want to search: 10
Element is present at index 3
30
8. QUICK SORT
8. PROGRAM:-
#include <stdio.h>
#include <conio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
int j,t;
31
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
void main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
clrscr();
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
getch();
}
SAMPLE OUTPUT:-
Before sorting array elements are-
24 9 29 14 19 27
After sorting array elements are -
9 14 19 24 27 29
32
9. OPERATIONS ON GRAPHS USING BFS & DFS
9. PROGRAM:-
#include<stdio.h>
#include<conio.h>
int a[10][10], n, m, i, j, source, s[10], b[10];
int visited[10];
void create()
{
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d", &a[i][j]);
}
void bfs()
{
int q[10], u, front=0, rear=-1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: ");
while(front<=rear)
{
u = q[front++];
for(i=1; i<=n; i++)
{
if(a[u][i] == 1 && visited[i] == 0)
{
q[++rear] = i;
visited[i] = 1;
printf("\n%d", i);
}
}
}
}
void dfs(int source)
{
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for(v=1; v<=n; v++)
33
{
if(a[source][v] == 1 && b[v] == 0)
{
printf("\n%d -> %d", source, v);
dfs(v);
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Create Graph\n2.BFS\n3.Check graph connected or not(DFS)\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create();break;
case 2: bfs();
for(i=1;i<=n;i++)
if(visited[i]==0)
printf("\nThe vertex that is not rechable %d" ,i);
break;
case 3: printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d",&source);
m=1;
dfs(source);
for(i=1;i<=n;i++)
{
if(b[i]==0)
m=0;
}
if(m==1)
printf("\nGraph is Connected");
else
printf("\nGraph is not Connected");
break;
default: exit(0);
}
}
}
34
SAMPLE OUTPUT:-
1. Create Graph 2.BFS 3.Check graph connected or not (DFS) 4.Exit
Enter your choice: 1
Enter the number of vertices of the digraph: 4
Enter the adjacency matrix of the graph:
0011
0000
0100
0100
35
10. MERGE SORT
10.PROGRAM:-
#include <stdio.h>
#include <conio.h>
/* Function to merge the subarrays of a[] */
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int b[10];
i = beg;
j = mid+1;
k = beg;
while (i <= mid && j <= end)
{
if(a[i] <= a[j])
{
b[k] = a[i];
i++;
}
else
{
b[k] = a[j];
j++;
}
k++;
}
if(i>mid)
{
while (j<=end)
{
b[k] = a[j];
j++;
k++;
}
}
else{
while (i<=mid)
{
b[k] = a[i];
i++;
k++;
}
36
}
for(k=beg;k<=end;k++)
a[k]=b[k];
}
void main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
clrscr();
printf("Before sorting array elements are - \n");
printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After merge sorting array elements are - \n");
printArray(a, n);
getch();
37
SAMPLE OUTPUT:-
Before sorting array elements are-
12 31 25 8 32 17 40 42
After merge sorting array elements are –
8 12 17 25 31 32 40 42
38