[go: up one dir, main page]

0% found this document useful (0 votes)
47 views5 pages

DM LAB Programs

The document contains 4 programming problems related to discrete mathematics concepts: 1. A C program to print the boolean truth table for logical operators AND, OR, and NOT. 2. A program to find the Cartesian product of two sets by taking the input sets and printing all combinations of their elements. 3. A program to perform set operations like union, intersection, and subtraction by taking input sets and applying the respective operation. 4. A recursive program to calculate the sum of digits of a given number.

Uploaded by

arunkumars0121
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)
47 views5 pages

DM LAB Programs

The document contains 4 programming problems related to discrete mathematics concepts: 1. A C program to print the boolean truth table for logical operators AND, OR, and NOT. 2. A program to find the Cartesian product of two sets by taking the input sets and printing all combinations of their elements. 3. A program to perform set operations like union, intersection, and subtraction by taking input sets and applying the respective operation. 4. A recursive program to calculate the sum of digits of a given number.

Uploaded by

arunkumars0121
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/ 5

1 2

23MA1102 DISCRETE MATHEMATICS FOR COMPUTER SCIENCE printf("{");


LIST OF EXPERIEMENTS for(int i=0;i<n1;i++)
1. WRITE A PROGRAM IN C TO DISPLAY THE BOOLEAN TRUTH TABLE FOR {
AND, OR , NOT for(int j=0;j<n2;j++)
Program: {
#include<stdio.h> printf(" (%d %d) ",a[i],b[j]);
int main() }
{ }
int a,b,i; printf("}");
printf("\t\tTruth Table\n\n"); return 0;
printf(" A\tB\tA.B\tA+B\t!A\t!B\n"); }
for(i = 0;i <= 3;i++)
{ Output:
a = i / 2; Enter size of set A
b = i % 2; 2
printf(" %d\t%d\t%d\t%d\t%d\t%d\n", a, b, a&&b, a||b, !a, !b); Enter element of set A
} 12
return 0; Enter size of set B
} 4
Output: Enter element of set B
Truth Table 4567
A B A.B A+B !A !B { (1 4) (1 5) (1 6) (1 7) (2 4) (2 5) (2 6) (2 7)}
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0 3. PRACTICE OF VARIOUS SET OPERATIONS

Program:
#include <stdio.h>
2. WRITE A C PROGRAM TO FIND CARTESIAN PRODUCT OF TWO SETS void main()
{
Program: int i,j,k,p,ch,n1,n2,set1[10],set2[10], set3[20],flag;
char wish;
#include<stdio.h> do
int main() {
{ printf("press 1 for union");
int a[10],b[10],n1,n2; printf("\n press 2 for intersection");
printf("Enter size of set A\n"); printf("\n press 3 for subtraction");
scanf("%d",&n1); printf("\n enter ur choice");
printf("Enter element of set A\n"); scanf("%d",&ch);
for(int i=0;i<n1;i++) switch(ch)
scanf("%d",&a[i]); {
printf("Enter size of set B\n"); case 1:
scanf("%d",&n2); printf("\n enter the size of set1 \n");
printf("Enter element of set B\n"); scanf("%d",&n1);
for(int i=0;i<n2;i++) printf("enter the element of set1\n");
scanf("%d",&b[i]); for(i=0;i<n1;i++)
scanf("%d",&set1[i]);
// logic for cartesian product printf("enter the size of set2\n");
scanf("%d",&n2);
3 4

printf("enter the elements of set2\n"); {


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

p=k; 4.b
for(k=0;k <p;k++) Program:
{ #include <stdio.h>
printf( "%d",set3[k]); int sum (int a);
} int main()
break; {
default: printf("completed"); int num, result;
} printf("Enter the number: ");
}while(ch<=3); scanf("%d", &num);
result = sum(num);
} printf("Sum of digits in %d is %d\n", num, result);
Output: return 0;
press 1 for union }
press 2 for intersection int sum (int num)
press 3 for subtraction {
enter ur choice 1 if (num != 0)
enter the size of set1 {
4 return (num % 10 + sum (num / 10));
enter the element of set1 }
5614 else
enter the size of set2 {
3 return 0;
enter the elements of set2 }
237 }
Union is Output:
1234567 Enter the number: 12345
Sum of digits 12345 is: 15

4. RECURSION AND INDUCTION 5. IMPLEMENTATION OF A RECURSIVE COUNTING TECHNIQUE


4.a. C program to find factorial of given number using Recursion
Program: Program:
#include <stdio.h>
// Function to find factorial of given number #include <stdio.h>
unsigned int factorial(unsigned int n)
{ //function to count digits
if (n == 1) int countDigits(int num)
{ {
return 1; static int count=0;
}
return n * factorial(n - 1); if(num>0)
} {
int main() count++;
{ countDigits(num/10);
int num = 5; }
printf("Factorial of %d is %d", num, factorial(num)); else
return 0; {
} return count;
Output: }
Factorial of 5 is 120 }
7 8

int main() {
{ for(j=1;j<=n;j++)
int number; {
int count=0; if(cost[i][j]<min)
{
printf("Enter a positive integer number: "); if(visited[i]!=0)
scanf("%d",&number); {
min=cost[i][j];
count=countDigits(number); a=i;
b=j;
printf("Total digits in number %d is: %d\n",number,count); }
}
return 0; }
} }
//if node is not visited
Output: if(visited[b]==0)
Enter a positive integer number: 12345 {
Total digits in number 12345 is: 5 printf("\n%d to %d cost=%d",a,b,min);
min_cost=min_cost+min;
no_e++;
6. WRITE A PROGRAM IN C FOR MINIMUM COST SPANNING TREE. }
visited[b]=1;
Program: cost[a][b]=cost[b][a]=1000;
}
#include<stdio.h> printf("\nminimum weight is %d",min_cost);
int main() return 0;
{ }
int cost[10][10],visited[10]={0},i,j,n,no_e=1,min,a,b,min_cost=0;
printf("Enter number of nodes "); Output:
scanf("%d",&n); Enter number of nodes 6
printf("Enter cost in form of adjacency matrix\n"); Enter cost in form of adjacency matrix
//input graph 044000
for(i=1;i<=n;i++) 402000
{ 420324
for(j=1;j<=n;j++) 003003
{ 002003
scanf("%d",&cost[i][j]); 004330
// cost is 0 then initialize it by maximum value 1 t0 o 2 cost=4
if(cost[i][j]==0) 2 to 3 cost=2
cost[i][j]=1000; 3 to 5 cost=2
} 3 to 4 cost=3
} 4 to 6 cost=3
minimum weight is 14
// logic for finding minimum cost spanning tree
visited[1]=1; // visited first node
while(no_e<n) 7. WRITE A PROGRAM IN C FOR FINDING SHORTEST PATH IN A GRAPH
{ NODE
min=1000;
// in each cycle find minimum cost WRITE A PROGRAM IN C FOR FINDING SHORTEST PATH IN A
for(i=1;i<=n;i++) GRAPHNOTE.
9 10

Program: {0, 3, 0, 0, 1, 0},


#include &lt;stdio.h&gt; {1, 2, 0, 0, 0, 4},
#include &lt;stdbool.h&gt; {0, 0, 1, 0, 0, 3},
#include &lt;limits.h&gt; {0, 0, 0, 4, 3, 0}
#define V 6 // Number of vertices in the graph };
// Function to find the vertex with the minimum distance value dijkstra(graph, 0);
int minDistance(int dist[], bool sptSet[]) { return 0;
int min = INT_MAX, min_index; }
for (int v = 0; v &lt; V; v++) {
if (!sptSet[v] &amp;&amp; dist[v] &lt; min) { Output:
min = dist[v]; Vertex Distance from Source
min_index = v; 00
} 12
} 25
return min_index; 31
} 46
// Function to print the constructed distance array 55
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},

You might also like