Discrete Math Practical Student - 05!04!2021
Discrete Math Practical Student - 05!04!2021
C Program:
#include <stdio.h>
int main()
{
int n = 3; // Number of disks
ToH(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
Output:
Move disk 1 from rod A to rod B
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
2. Write the programs using “C” for Graph representation using Adjacency List.
C Program:
// A C Program to demonstrate adjacency list representation of graphs
#include <stdio.h>
#include <stdlib.h>
return 0;
}
Output:
Adjacency list of vertex 0
head -> 1-> 4
Adjacency list of vertex 1
head -> 0-> 2-> 3-> 4
Adjacency list of vertex 2
head -> 1-> 3
Adjacency list of vertex 3
head -> 1-> 2-> 4
Adjacency list of vertex 4
head -> 0-> 1-> 3
3. Write the programs using “C” for Graph representation using Adjacency Matrix.
C Program:
#include <stdio.h>
// Update value to 1
Adj[x][y] = 1;
Adj[y][x] = 1;
}
}
// Driver Code
int main()
{
// Number of vertices
N = 5;
// Given Edges
int arr[][2]
= { { 1, 2 }, { 2, 3 },
{ 4, 5 }, { 1, 5 } };
// Number of Edges
M = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Output:
01001
10100
01000
00001
10010
4. Write the programs using “C” for String Matching using finite state machine.
C Program:
// C program for Finite Automata Pattern searching Algorithm
#include<stdio.h>
#include<string.h>
#define NO_OF_CHARS 256
return 0;
}
int TF[M+1][NO_OF_CHARS];
computeTF(pat, M, TF);
Output:
Pattern found at index 0
Pattern found at index 9
Pattern found at index 13
7. Write the programs using “C” to find the power set for a given set.
#include <stdio.h>
#include <math.h>
8. Write the programs using “C” to find GCD of two numbers using recursion.
// C program to find GCD of two numbers
#include <stdio.h>
// base case
if (a = = b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
// Recur
return binomialCoeff(n - 1, k - 1)
+ binomialCoeff(n - 1, k);
}
Output:
Value of C(5, 2) is 10
10. Write the programs using “C” to find Permutation and Combination result for a given
pair of values n and r.
// CPP program To calculate The Value Of nCr
#include <bits/stdc++.h>
using namespace std;
// Returns factorial of n
int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Driver code
int main()
{
int n = 5, r = 3;
cout << nCr(n, r);
return 0;
}
11. Write the programs using “C” to check a number is prime or not.
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Output