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

DM LAB Programs

The document contains a list of programming experiments for a Discrete Mathematics course, focusing on various topics such as Boolean truth tables, Cartesian products, set operations, recursion, and algorithms for minimum cost spanning trees and shortest paths in graphs. Each experiment includes a C program, its logic, and sample outputs. The programs demonstrate fundamental concepts in computer science and discrete mathematics through practical coding exercises.

Uploaded by

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

DM LAB Programs

The document contains a list of programming experiments for a Discrete Mathematics course, focusing on various topics such as Boolean truth tables, Cartesian products, set operations, recursion, and algorithms for minimum cost spanning trees and shortest paths in graphs. Each experiment includes a C program, its logic, and sample outputs. The programs demonstrate fundamental concepts in computer science and discrete mathematics through practical coding exercises.

Uploaded by

sriram98713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

23MA1102 DISCRETE MATHEMATICS FOR COMPUTER SCIENCE

LIST OF EXPERIEMENTS
1. WRITE A PROGRAM IN C TO DISPLAY THE BOOLEAN TRUTH TABLE FOR
AND, OR , NOT
Program:
#include<stdio.h>
int main()
{
int a,b,i;
printf("\t\tTruth Table\n\n");
printf(" A\tB\tA.B\tA+B\t!A\t!B\n");
for(i = 0;i <= 3;i++)
{
a = i / 2;
b = i % 2;
printf(" %d\t%d\t%d\t%d\t%d\t%d\n", a, b, a&&b, a||b, !a, !b);
}
return 0;
}
Output:
Truth Table
A B A.B A+B !A !B
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0

2. WRITE A C PROGRAM TO FIND CARTESIAN PRODUCT OF TWO SETS

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]);

// logic for cartesian product


printf("{");
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
printf(" (%d %d) ",a[i],b[j]);
}
}
printf("}");
return 0;
}

Output:
Enter size of set A
2
Enter element of set A
12
Enter size of set B
4
Enter element of set B
4567
{ (1 4) (1 5) (1 6) (1 7) (2 4) (2 5) (2 6) (2 7)}

3. PRACTICE OF VARIOUS SET OPERATIONS

Program:
#include <stdio.h>
void main()
{
int i,j,k,p,ch,n1,n2,set1[10],set2[10], set3[20],flag;
char wish;
do
{
printf("press 1 for union");
printf("\n press 2 for intersection");
printf("\n press 3 for subtraction");
printf("\n enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the size of set1 \n");
scanf("%d",&n1);
printf("enter the element of set1\n");
for(i=0;i<n1;i++)
scanf("%d",&set1[i]);
printf("enter the size of set2\n");
scanf("%d",&n2);
printf("enter the elements of set2\n");
for(i=0;i<n2;i++)
scanf("%d",&set2[i]);
k=0;
for(i=0;i<n1;i++)
{
set3[k]=set1[i];
k++;
}
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=set2[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf( "%d",set3[k]);
}
break;

case 2:
printf("Intersection");
printf("enter the size of sets1");
scanf("%d",&n1);
printf("enter the element of set1");
for(i=0;i<n1;i++)
scanf("%d",&set1[i]);
printf("enter the size of sets2");
scanf("%d",&n2);
printf("enter the elements of set2");
for(i=0;i<n2;i++)
scanf("%d",&set2[i]);
k=0;
for(i=0;i<n2;i++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(set2[i]==set1[j])
{
flag=0;
break;
}
}
if(flag==0)
{
set3[k]=set2[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf( "%d",set3[k]);
}
break;

case 3:
printf("Set Difference");
printf("enter the size of sets1");
scanf("%d",&n1);
printf("enter the element of set1");
for(i=0;i<n1;i++)
scanf("%d",&set1[i]);
printf("enter the size of sets2");
scanf("%d",&n2);
printf("enter the elements of set2");
for(i=0;i<n2;i++)
scanf("%d",&set2[i]);
k=0;
for(i=0;i<n1;i++)
{
flag=1;
for(j=0;j<n2;j++)
{
if(set1[i]==set2[j])
{
flag=0;
break;
}
}
if(flag==1)
{
set3[k]=set1[i];
k++;
}
}
p=k;
for(k=0;k <p;k++)
{
printf( "%d",set3[k]);
}
break;
default: printf("completed");
}
}while(ch<=3);

}
Output:
press 1 for union
press 2 for intersection
press 3 for subtraction
enter ur choice 1
enter the size of set1
4
enter the element of set1
5614
enter the size of set2
3
enter the elements of set2
237
Union is
1234567

4. RECURSION AND INDUCTION


4.a. C program to find factorial of given number using Recursion
Program:
#include <stdio.h>
// Function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Output:
Factorial of 5 is 120
4.b
Program:
#include <stdio.h>
int sum (int a);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
int sum (int num)
{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
}
Output:
Enter the number: 12345
Sum of digits 12345 is: 15

5. IMPLEMENTATION OF A RECURSIVE COUNTING TECHNIQUE

Program:

#include <stdio.h>

//function to count digits


int countDigits(int num)
{
static int count=0;

if(num>0)
{
count++;
countDigits(num/10);
}
else
{
return count;
}
}
int main()
{
int number;
int count=0;

printf("Enter a positive integer number: ");


scanf("%d",&number);

count=countDigits(number);

printf("Total digits in number %d is: %d\n",number,count);

return 0;
}

Output:
Enter a positive integer number: 12345
Total digits in number 12345 is: 5

6. WRITE A PROGRAM IN C FOR MINIMUM COST SPANNING TREE.

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;
cost[a][b]=cost[b][a]=1000;
}
printf("\nminimum weight is %d",min_cost);
return 0;
}

Output:
Enter number of nodes 6
Enter cost in form of adjacency matrix
044000
402000
420324
003003
002003
004330
1 t0 o 2 cost=4
2 to 3 cost=2
3 to 5 cost=2
3 to 4 cost=3
4 to 6 cost=3
minimum weight is 14

7. WRITE A PROGRAM IN C FOR FINDING SHORTEST PATH IN A GRAPH


NODE

WRITE A PROGRAM IN C FOR FINDING SHORTEST PATH IN A


GRAPHNOTE.
Program:
#include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;limits.h&gt;
#define V 6 // Number of vertices in the graph
// Function to find the vertex with the minimum distance value
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v &lt; V; v++) {
if (!sptSet[v] &amp;&amp; dist[v] &lt; min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}
// Function to print the constructed distance array
void printSolution(int dist[]) {
printf(&quot;Vertex \t Distance from Source\n&quot;);
for (int i = 0; i &lt; V; i++) {
printf(&quot;%d \t %d\n&quot;, i, dist[i]);
}
}
// Function to implement Dijkstra&#39;s algorithm to find the shortest path
void dijkstra(int graph[V][V], int src) {
int dist[V];
bool sptSet[V];
for (int i = 0; i &lt; V; i++) {
dist[i] = INT_MAX;
sptSet[i] = false;
}
dist[src] = 0;
for (int count = 0; count &lt; V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v &lt; V; v++) {
if (!sptSet[v] &amp;&amp; graph[u][v] &amp;&amp; dist[u] != INT_MAX
&amp;&amp;
dist[u] + graph[u][v] &lt; dist[v]) {

dist[v] = dist[u] + graph[u][v];


}
}
}
printSolution(dist);
}
int main() {
int graph[V][V] = {
{0, 2, 0, 1, 0, 0},
{2, 0, 3, 2, 0, 0},
{0, 3, 0, 0, 1, 0},
{1, 2, 0, 0, 0, 4},
{0, 0, 1, 0, 0, 3},
{0, 0, 0, 4, 3, 0}
};
dijkstra(graph, 0);
return 0;
}

Output:
Vertex Distance from Source
00
12
25
31
46
55

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy