DSTL Experiments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Experiment -6

Write a program to implement truth table for


AND,OR,NOT.

#include<stdio.h>

// logic for OR gate

int find_OR(int x,int y)

if(x==1 && y==1)

return 1;

if(x==1 && y==0 || x==0 && y==1)

return 1;

if(x==0 && y==0)

return 0;

int find_AND(int x,int y)

if(x==1 && y==1)

return 1;

else

return 0;

// logic for find NOT

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:
Experiment -7
Write a program in c to find the cartesian product .

#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:
Experiment-8
Write a program in c to implement relational and check
whether it is reflexive or not.

#include<stdio.h>

void main()

int a1[100],a2[100],i,j,n,n1,m[100][100],c=0;

printf("enter the size of an set 1\n");

scanf("%d",&n);

printf("enter the elements in set 1\n");

for(i=0;i<n;i++)

scanf("%d",&a1[i]);

printf("enter the size of an set 2\n");

scanf("%d",&n1);

printf("enter the elements in set 2\n");

for(j=0;j<n1;j++)

scanf("%d",&a2[j]);

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(a1[i]<a2[j]||a1[i]==a2[j])

{
m[i][j]=1;

else

m[i][j]=0;

printf("relational matrix is:\n");

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

printf("%d ",m[i][j]);

printf("\n");

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(i==j && m[i][j]==1)

c++;

if(c==n)

printf("relational matrix is reflexive\n");

else
printf("relational matrix is not reflexive\n");

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(m[i][j]==1&&m[j][i]==1)

c++;

Output:
Experiment -9
Write a program to implement relational and check
whether it is symmetric or not.

#include<stdio.h>

void main()

int a1[100],a2[100],i,j,n,n1,m[100][100],c=0;

printf("enter the size of an set 1\n");

scanf("%d",&n);

printf("enter the elements in set 1\n");

for(i=0;i<n;i++)

scanf("%d",&a1[i]);

printf("enter the size of an set 2\n");

scanf("%d",&n1);

printf("enter the elements in set 2\n");

for(j=0;j<n1;j++)

scanf("%d",&a2[j]);

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(a1[i]<a2[j]||a1[i]==a2[j])

{
m[i][j]=1;

else

m[i][j]=0;

printf("relational matrix is:\n");

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

printf("%d ",m[i][j]);

printf("\n");

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(m[i][j]==1&&m[j][i]==1)

c++;

if(c==n*n1)

printf("relational matrix is symetric\n");

else

printf("relational matrix is not symetrix\n");


for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(m[i][j]==m[j][i])

if(i==j)

c++;

Output:

Experiment-10
Write a program to implement relational matrix and
check whether it is antisymmetric or not.
#include<stdio.h>

void main()

int a1[100],a2[100],i,j,n,n1,m[100][100],c=0;

printf("enter the size of an set 1\n");

scanf("%d",&n);

printf("enter the elements in set 1\n");

for(i=0;i<n;i++)

scanf("%d",&a1[i]);

printf("enter the size of an set 2\n");

scanf("%d",&n1);

printf("enter the elements in set 2\n");

for(j=0;j<n1;j++)

scanf("%d",&a2[j]);

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(a1[i]<a2[j]||a1[i]==a2[j])

m[i][j]=1;

}
else

m[i][j]=0;

printf("relational matrix is:\n");

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

printf("%d ",m[i][j]);

printf("\n");

for(i=0;i<n;i++)

for(j=0;j<n1;j++)

if(m[i][j]==m[j][i])

if(i==j)

c++;

}
if(c==n)

printf("relational matrix is antisymetric\n");

else

printf("relational matrix is not antisymetrix\n");

Output:

Experiment-11
Write a program to implement prim’s
algorithm(spanning tree).
#include<stdio.h>

#include<conio.h>

int a,b,u,v,n,i,j,ne=1;

int visited[10]={0},min,mincost=0,cost[10][10];

void main()

printf("\nEnter the number of nodes:");

scanf("%d",&n);

printf("\nEnter the adjacency matrix:\n");

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;

visited[1]=1;

printf("\n");

while(ne < n)

for(i=1,min=999;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]< min)

if(visited[i]!=0)

min=cost[i][j];

a=u=i;

b=v=j;

}
if(visited[u]==0 || visited[v]==0)

printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);

mincost+=min;

visited[b]=1;

cost[a][b]=cost[b][a]=999;

printf("\n Minimun cost=%d",mincost);

getch();

Output:

Experiment-12
Write a program implement krushkal’s alogorithm to
find the shortest path.
#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

int i, j, k, a, b, u, v, n, ne = 1;

int min, mincost = 0, cost[9][9], parent[9];

int find(int);

int uni(int, int);

void main()

printf("Kruskal's algorithm in C\n");

printf("========================\n");

printf("Enter the no. of vertices:\n");

scanf("%d", &n);

printf("\nEnter the cost adjacency matrix:\n");

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("The edges of Minimum Cost Spanning Tree are\n");

while (ne < n)

for (i = 1, min = 999; i <= n; i++)

{
for (j = 1; j <= n; j++)

if (cost[i][j] < min)

min = cost[i][j];

a = u = i;

b = v = j;

u = find(u);

v = find(v);

if (uni(u, v))

printf("%d edge (%d,%d) =%d\n", ne++, a, b, min);

mincost += min;

cost[a][b] = cost[b][a] = 999;

printf("\nMinimum cost = %d\n", mincost);

getch();

int find(int i)

while (parent[i])

i = parent[i];

return i;

int uni(int i, int j)

if (i != j)
{

parent[j] = i;

return 1;

return 0;

Output:

Experimrnt:16
Write a program to implement binomial expansion.
#include<stdio.h>

int factorial(int n)

int f = 1;

for (int i = 2; i <= n; i++)

f *= i;

return f;

void series(int A, int X, int n)

int nFact = factorial(n);

printf("binomial expansion nCr is:\n");

for (int i = 0; i < n + 1; i++)

int niFact = factorial(n - i);

int iFact = factorial(i);

int aPow = pow(A, n - i);

int xPow = pow(X, i);

printf("%d\t",(nFact * aPow * xPow) /

(niFact * iFact) );

int main()

int A = 3, X = 4, n = 5;

series(A, X, n);

return 0;

Output:

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