DSTL - Lab Manual
DSTL - Lab Manual
THEORY OF LOGIC
(KCS - 353)
LABORATORY MANUAL
COMPUTER SCIENCE
&
ENGINEERING
M3. To impart human values and ethics in students, to make them socially and
eco-friendly responsible.
Vision of the Department
M1. "To impart quality education to the students to enhance their ethical,
professional and leadership qualities to make them globally competitive."
M3. "To strive for continual enhancement of technical knowledge & innovation
through industry interface to accomplish global needs."
Program Educational Objectives (PEOs)
PEO2: Students must be able to analyze, design, and implement the latest
technology-driven projects.
PSO 1: Able to design and implement the data structures and algorithms to deliver
quality software products.
1. Solve the practical examples of sets, functions, relations and recurrence relations.
2. Describe the concept of logical and mathematical foundations and study abstract
models of computation.
3. Define modern algebra for constructing and writing mathematical proofs.
4. Recognize the patterns that arise in graph problems and use this knowledge for
constructing the trees and spanning trees.
Bloom's
Course Outcomes:
Level (BL)
4 Write a program in C to create two sets and perform the Symmetric CO2 L4
Difference operation.
7 Write a program in C to Display the Boolean Truth Table for AND, OR CO3 L4
, NOT .
8 Write a program in C for Minimum Cost Spanning Tree. CO4 L4
Aim: Write a program in C to create two sets and perform the Union operation on sets.
Content: Set Operations include Set Union, Set Intersection, Set Difference, Complement of
Set, and Cartesian Product.
Union of two sets :-
If A={1,1,2,3} and B={5,6,7,8} are two sets, then union of the set A and B is :-
A ∪ B = {1,2,3,5,6,7,8}
Program:-
Here A and B is the two sets and C is the union of set A and B.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],i,c[10],j,k=0,n1,n2;
Output:
Screenshot
EXPERIMENT No. 2
Aim: Write a program in C to create two sets and perform the Intersection operation on sets.
Content:
Intersection of two sets :- Intersection of two sets is the set in which only those element is
present which are present in both set A and set B.
If A={1,2,3,4,1} and B={1,2,5,6} are two sets then intersection of A and B is:-
A∩B = {1,2}
Program:-
Here A and B is the two sets and C is the intersection of set A and B.
#include<stdio.h>
int main()
{
int a[100],b[100],c[100],n1,n2,n,k=0,i,j;
for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(a[i]==b[j])
{
c[k]=a[i];
k++;
}
}
return 0;
}
Output:
Screenshot
EXPERIMENT No. 3
Aim: Write a program in C to create two sets and perform the Difference operation on sets.
Content:
If A and B are two sets, then their difference is given by A - B or B - A.
If A = {2, 3, 4} and B = {4, 5, 6},
(i) A - B means elements of A which are not the elements of B.
i.e., in the above example A - B = {2, 3}
(ii) B -A means elements of B which are not the elements of A.
i.e., in the above example B -A = {5, 6}
Program:-
Here A and B is the two sets and C is the difference of set A and B.
#include<stdio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n1,n2,l,i,j;
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set A");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set B");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);
for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{
// here we check that is element already present in the set
// if present than ignore it otherwise add to the difference set
for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}
}
for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
// here we check that is element already present in the set
//if present than ignore it otherwise add to the difference set
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}
}
printf("Difference of A-B is:-\n");
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
printf("\n");
printf("Difference of B-A is:-\n");
for(i=0;i<m;i++)
{
printf("%d ",d[i]);
}
return 0;
}
Output:
Screenshot
EXPERIMENT No. 4
Aim: Write a program in C to create two sets and perform the Symmetric difference operation
on sets.
Content:
Symmetric difference of the two set is:
Let the set M = {2, 6, 8, 12, 19, 23, 27, 54} and set N = {4, 5, 10, 24, 19, 27, 36, 49}
M-N = {2, 6, 8, 12, 23, 54} (If set N elements remove from set M and the remaining
elements left in set M write it)
N-M = {4, 5, 10, 24, 36, 49} (If set M elements remove from set N and the remaining
elements left in set N write it)
Therefore, MΔN = (M-N) U (N-M) = {2, 6, 8, 12, 23, 54} U {4, 5, 10, 24, 36, 49}
= {2, 4, 5, 6, 8, 10, 12, 23, 24, 36, 49, 54}
Program:-
#include<stdio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n=0,n1,n2,l,i,j,sy[100];
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set A");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set B");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);
Output:
Screenshot
EXPERIMENT No. 5
Content: Power Set Power set P(S) of a set S is the set of all subsets of S. For example S =
{a, b, c} then P(S) = {{}, {a}, {b}, {c}, {a,b}, {a, c}, {b, c}, {a, b, c}}.
If S has n elements in it then P(S) will have 2n elements
Program:-
#include <stdio.h>
#include <math.h>
void printPowerSet(char *set, int set_size)
{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
unsigned int pow_set_size = pow(2, set_size);
int counter, j;
return 0;
}
Output:
Screenshot
EXPERIMENT No. 6
Content:
Cartesian Product of two set:-
If set A={1,2} and set B={4,5,6,7} then cartesian product of set A and B is :-
A*B={ (1,4),(1,5),(1,6),(1,7),(2,4),(2,5),(2,6),(2,7) }
Program:-
#include<stdio.h>
int main()
{
int a[10],b[10],n1,n2;
printf("Enter size of set A\n");
scanf("%d",&n1);
printf("Enter element of set A\n");
for(int i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B\n");
scanf("%d",&n2);
printf("Enter element of set B\n");
for(int i=0;i<n2;i++)
scanf("%d",&b[i]);
Output:
Screenshot
EXPERIMENT No. 7
Aim: Write a program in C to Display the Boolean Truth Table for AND, OR, NOT.
Content:
A Logic gate is an elementary building block of any digital circuits. It takes one or two inputs and
produces output based on those inputs. Outputs may be high (1) or low (0).
In a computer, most of the electronic circuits are made up logic gates. Logic gates are used to create a
circuit that performs calculations, data storage or shows off object-oriented programming especially the
power of inheritance.
There are seven basic logic gates defined, these are:
1. AND gate,
2. OR gate,
3. NOT gate,
4. NAND gate,
5. NOR gate,
6. XOR gate and
7. XNOR gate.
Below are the brief details about them (AND, OR, NOT gates):
1. AND Gate: The AND gate gives an output of 1 if both the two inputs are 1 and gives 0 otherwise.
2. OR Gate: The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise.
3. NOT Gate: It acts as an inverter. It takes only one input. If the input is given as 1, it will invert the
result as 0 and vice-versa.
Program:-
#include<stdio.h>
int find_NOT(int x)
{
if(x==1)
return 0;
else
return 1;
}
// Driver function
int main()
{
int ch,a,b;
printf("1. OR\n");
printf("2. AND\n");
printf("3. NOT\n");
printf("4 .exit\n");
while(1)
{
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Give two input 1 for true and 0 for false\n");
scanf("%d%d",&a,&b);
printf("%d",find_OR(a,b));
break;
case 2: printf("Give two input 1 for true and 0 for false\n");
scanf("%d%d",&a,&b);
printf("%d",find_AND(a,b));
break;
case 3: printf("Give an input 1 for true and 0 for false\n");
scanf("%d",&a);
printf("%d",find_NOT(a));
break;
case 4: exit(0);
default: printf("Wrong key\n");
}
}
}
Output:
Screenshot
EXPERIMENT No. 8
Content:
Given an undirected graph of V nodes (V > 2) named V1, V2, V3, …, Vn.
Two nodes Vi and Vj are connected to each other if and only if 0 < | i – j | ≤ 2. Each edge
between any vertex pair (Vi, Vj) is assigned a weight i + j. The task is to find the cost of
the minimum spanning tree of such graph with V nodes. Graph should be weighted,
connected, and undirected.
Example:
Input: V = 4
Output: 13
Program:-
#include<stdio.h>
int main()
{
int cost[10][10],visited[10]={0},i,j,n,no_e=1,min,a,b,min_cost=0;
printf("Enter number of nodes ");
scanf("%d",&n);
printf("Enter cost in form of adjacency matrix\n");
//input graph
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
// cost is 0 then initialize it by maximum value
if(cost[i][j]==0)
cost[i][j]=1000;
}
}
// logic for finding minimum cost spanning tree
visited[1]=1; // visited first node
while(no_e<n)
{
min=1000;
// in each cycle find minimum cost
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i]!=0)
{
min=cost[i][j];
a=i;
b=j;
}
}
}
}
//if node is not visited
if(visited[b]==0)
{
printf("\n%d to %d cost=%d",a,b,min);
min_cost=min_cost+min;
no_e++;
}
visited[b]=1;
// initialize with maximum value you can also use any other value
cost[a][b]=cost[b][a]=1000;
}
printf("\nminimum weight is %d",min_cost);
return 0;
}
Output:
Screenshot
EXPERIMENT No. 9
Content:
We are given a graph with a source vertex in the graph. And we have to find the shortest path
from the source vertex to all other vertices of the graph.
The Dijikstra’s algorithm is a greedy algorithm to find the shortest path from the source
vertex of the graph to the root node of the graph.
Program:-
#include<stdio.h>
int main()
{
int cost[10][10],i,j,n,source,target,visited[10]={0},min=999,dist[10],pre[10];
int start,m,d,path[10];
printf("Enter number of nodes\n ");
scanf("%d",&n);
printf("Enter weight of all the paths in adjacency matrix form\n");
// Input graph
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("Enter the source\n");
scanf("%d",&source);
printf("Enter the target\n");
scanf("%d",&target);
Output:
Screenshot
EXPERIMENT No. 10
Content:
In C programming language, nCr is referred as the combination. nCr is the selection of r
objects from a set of n objects, where the order of objects does not matter.
nP is referred as the permutation. nP is arrangement of 'r' objects from a set of 'n' objects,
r r
which should be in an order or sequence.
Program:-
Following is the C program to find the permutation and combination of given numbers –
#include <stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
int main(){
int n, r;
long ncr, npr;
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
return 0;
}
long find_ncr(int n, int r) {
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r) {
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
Output:
Screenshot