1.final DiscreteLabManual S Shah

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

INDIRA GANDHI DELHI TECHNICAL UNIVERSITY FOR WOMEN

DISCRETE STRUCTURES LAB


CODE (MIW-552)
PRACTICAL FILE

SUBMITTED TO: SUBMITTED BY:


Mr. A.K. Mohapatra Shubhra Shah
ICT-2nd Semester
(01403152017)

1|P a g e S.Shah(Roll no 014) Discrete Structure Lab


LIST OF EXPERIMENTS
Course Code: MIW-552
Course Title: Discrete Structures Lab

Experiment
No. Title Remarks

Write a Program to calculate ab mod n for large a and


1
b without using power function.

2 Write a Program to calculate GCD.

Write a program to calculate prime number and draw


3
its distribution graph.

Write a program to calculate nCr and nPr of a given


4
number (permutation and combination).

Write a program to construct DFA that accept a string


5
ending at 00.

Write a program to construct DFA that accept a string


6
starting at 0 and ending at 1.

Write a program to identify whether the expression


7
entered has correct number of parentheses.

Write a program to find whether the relation is


8 reflexive and symmetric for a given input in form of
matrix.

9 Random number- A Case Study

2|P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 1

Aim: Write a Program to calculate ab mod n for large a and b without using
power function.

Program:

/* Write a program to calculate ab mod n for large numbers */


# include <stdio.h>
# include <conio.h>
main()
{
int a, b, i, p=1, n=7;
//clrscr();
printf("\n Enter the value of a");
scanf("%d",&a);
printf("\n Enter the value of b");
scanf("%d",&b);
for(i=1;i<=b;i++)
{
p=p*a;
}
p=p%n;
printf("\n product is= %d",p);
getch();
return 0;
}

3|P a g e S.Shah(Roll no 014) Discrete Structure Lab


Output:

Result: ab mod n is calculated for large a and b without using power function.
It gives large output in negative which is unable to store so we change it by
using Mod n in next program.

Program:

/* Write a program to calculate ab mod n for large numbers */


# include <stdio.h>
# include <conio.h>
main()
{
int a, b, i, p=1, n=7;
//clrscr();
printf("\n Enter the value of a");
scanf("%d",&a);

4|P a g e S.Shah(Roll no 014) Discrete Structure Lab


scanf("%d",&a);
printf("\n Enter the value of b");
scanf("%d",&b);
for(i=1;i<=b;i++)
{ p=(p*a)%n;
}
printf("\n product is= %d",p);
getch();
return 0;
}}

Output:

Result: ab mod n is calculated for large a and b without using power function.

5|P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 2

Aim: To calculate GCD (Greatest Common Divisor) for large value numbers
say ‘a’ & ‘b’.

Program:

/* Experiment: Write a program to calculate GCD for large numbers */


# include <stdio.h>
# include <conio.h>
main()
{
int n1, n2, temp;
//clrscr();
printf("\n Enter the value of n1 = ");
scanf("%d",&n1);
printf("\n Enter the value of n2 = ");
scanf("%d",&n1);
while (n2!=0)
{
temp=n2;
n2=n1%temp;
n1=temp;
}
printf("\n GCD is= %d",n1);
getch();
return 0;
}

6|P a g e S.Shah(Roll no 014) Discrete Structure Lab


Output:

Result: GCD (Greatest Common Divisor) is calculated for large value numbers.

7|P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 3

Aim: Write a program to calculate prime number and draw its distribution
graph.

Program:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num1, num2, i, j, flag, temp, count = 0;
//clrscr();
printf("Enter the value of num1 and num2 \n");
scanf("%d %d", &num1, &num2);
if (num2 < 2)
{
printf("There are no primes upto %d\n", num2);
exit(0);
}
// printf("Prime numbers are \n");
temp = num1;
if ( num1 % 2 == 0)
{
num1++;
}
for (i = num1; i <= num2; i = i + 2)
{
flag = 0;
for (j = 2; j <= i / 2; j++)
{
if ((i % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
// printf("%d\n", i);
count++;

}
}

8|P a g e S.Shah(Roll no 014) Discrete Structure Lab


printf("Number of primes between %d & %d = %d\n", temp, num2, count);
}

Output:

Result: Prime numbers count is identified for a particular range of input.

9|P a g e S.Shah(Roll no 014) Discrete Structure Lab


10 | P a g e S.Shah(Roll no 014) Discrete Structure Lab
Experiment No. 4

Aim: Write a program to calculate nCr and nPr of a given number (permutation
and combination).

Program:

// To find nPr

#include<stdio.h>

int factorial(int n)
{
int f;
For(f=1;n>1;n--)
F*=n;
Return f;
}

int npr(int n, int r)


{
Return factorial(n)/factorial(n-r);
}

int main()
{
int n,r;
clrscr();
printf(“\n Enter the value of n and r for nPr);
printf(“\n n=”);
scanf(“%d”, &n);
printf(“\n r=”);
scanf(“%d” ,&r);
printf(“\n %dp%d = %d \n”,n,r,npr(n,r));
return 0;
}

11 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Output:

Result: nPr is calculated for the given input of n and r.

Program:

// To find nCr
#include<stdio.h>
int Factorial(int x)
{
int factorial = 1, count;
for(count = x; count > 1; count--)
{
factorial = factorial * count;
}
return factorial;
}

12 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


int main()
{
int n, r;
float result;
printf("Enter value of N:\t");
scanf("%d", &n);
printf("Enter value of R:\t");
scanf("%d", &r);
result = (float)Factorial(n) / (Factorial(r) * Factorial(n - r));
printf("\nValue of nCr:\t %.2f\n", result);
return 0;
}

Output:

Result: nCr is calculated for the given input of n and r.

13 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 5

Aim: Write a program to construct DFA that accept a string ending at 00.

Program:

#define max 100


#include <stdio.h>
main()
{
char str[max],f='a';
int i;
clrscr();
printf("\n\n\nenter the string to be checked: ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
switch(f)
{
case 'a': if(str[i]=='0') f='b';
else if(str[i]=='1') f='a';
break;
case 'b': if(str[i]=='0') f='c';
else if(str[i]=='1') f='a';
break;
case 'c': if(str[i]=='0') f='c';
else if(str[i]=='1') f='a';
break;

}
}
if(f=='c') printf("\nString is accepted as it reached the final state %c at the
end.",f);
else printf("\nString is not accepted as it reached %c which is not the final
state.",f);
}

14 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Output:

Result: The DFA is constructed that takes inputs as string of 0 and 1 and returns
“String is accepted as it reached the final state c at the end “ if the input string
ends in 00.

15 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 6

Aim: Write a program to construct DFA that accept a string starting at 0 and
ending at 1.

Program:

//Construct a DFA , starting at '0' and ending at '1'


#define max 100
#include<stdio.h>
void main()
{
char str[max],f='a';
int i;
// clrscr();
printf("\n\nenter the string to be checked: ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
switch(f)
{
case 'a': if(str[i]=='0') f='b';
else if(str[i]=='1')
exit();
break;
case 'b': if(str[i]=='0') f='b';
else if(str[i]=='1') f='c';
break;
case 'c': if(str[i]=='0') f='b';
else if(str[i]=='1') f='c';
break;
}
}
if(f=='c')
printf("\nString is accepted as it reached the final state %c at the end.",f);
else
printf("\nString is not accepted as input given was 1 or output state is not 1");
}

16 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Output:

Result: The DFA is constructed that takes inputs as string of 0 and 1 and returns
“String is accepted as it reached the final state c at the end “if the string starts at
0 and ends at 1.

17 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 7

Aim: Write a program to identify whether the expression entered has correct
number of parentheses.

Program:

//C Program to Check if Expression is correctly Parenthesized

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top = -1;


char stack[100];

// function prototypes
void push(char);
void pop();
void find_top();

void main()
{
int i;
char a[100];
printf("enter expression\n");
scanf("%s", &a);
for (i = 0; a[i] != '\0';i++)
{
if (a[i] == '(')
{
push(a[i]);
}
else if (a[i] == ')')
{
pop();
}
}
find_top();

18 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


// to push elements in stack
void push(char a)
{
stack[top] = a;
top++;
}

// to pop elements from stack


void pop()
{
if (top == -1)
{
printf("expression is invalid\n");
exit(0);
}
else
{
top--;
}
}

// to find top element of stack


void find_top()
{
if (top == -1)
printf("\nexpression is valid\n");
else
printf("\nexpression is invalid\n");
}

19 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Output:

Result: Expression is checked for correctly Parenthesized.

20 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 8

Aim: Write a program to find whether the relation is symmetric for a given
input in form of matrix.

Program:

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10][10],i,j,m;
clrscr();
printf("Enter order of square matrix: ");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
printf("Enter value of a[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if(a[i][j]!=a[j][i])
{
printf("\n\nMatrix is not symmetric");
getch();
exit(0);
}
}
}
printf("\n\nMatrix is symmetric");

getch();

21 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Output:

Result: Relation is checked for symmetric in form of a matrix.

include<conio.h>
#include<stdio.h>
void main()
{
int a[10][10],i,j,m;
clrscr();
printf("Enter order of square matrix: ");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
printf("Enter value of a[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=m;i++)
{

if(a[i][i]!=1)
{
printf("\n\nMatrix is not reflexive");
getch();

22 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


exit(0);
}

}
printf("\n\nMatrix is reflexive");

getch();

Result: Relation is checked for reflexive in form of a matrix.

23 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Experiment No. 9

Random Number- A Case Study

Random number generation is the generation of a sequence


of numbers or symbols that cannot be reasonably predicted better than by
a random chance, usually through a hardware random-number generator
(RNG).There are two principal methods used to generate random numbers.
● The first method measures some physical phenomenon that is expected to
be random and then compensates for possible biases in the measurement
process. Example sources include measuring atmospheric noise, thermal noise,
and other external electromagnetic and quantum phenomena. For example,
cosmic background radiation or radioactive decay as measured over short
timescales represents sources of natural entropy.
● The second method uses computational algorithms that can produce long
sequences of apparently random results, which are in fact completely
determined by a shorter initial value, known as a seed value or key. As a result,
the entire seemingly random sequence can be reproduced if the seed value is
known. This type of random number generator is often called a pseudorandom
number generator (PSRN). This type of generator typically does not rely on
sources of naturally occurring entropy, though it may be periodically seeded by
natural sources. This generator type is non-blocking, so they are not rate-limited
by an external event, making large bulk reads a possibility.

1. Properties of Random Numbers


● A sequence of random numbers, must have two important
properties:
1. uniformity, i.e. they are equally probable every where

24 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


2. independence, i.e. the current value of a random variable has no
relation with the previous values
● C program to generate pseudo-random numbers using rand and random
function (Turbo C compiler only). As the random numbers are generated
by an algorithm used in a function they are pseudo-random, this is the
reason that word pseudo is used. Function rand() returns a pseudo-
random number between 0 and RAND_MAX. RAND_MAX is a constant
which is platform dependent and equals the maximum value returned by
rand function

25 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


Aim: Write a program to print pseudo random numbers in range [0, 100]. So we
calculate rand() % 100 which will return a number in [0, 99] so we add 1 to get
the desired range

Program:

#include <stdio.h>
#include <stdlib.h>

// Driver program
int main(void)
{
// This program will create same sequence of
// random numbers on every program run
int i;

printf("\n")
for(i = 0; i<5; i++)
printf(" %d ", rand());
return 0;
}

Output

Result: If you rerun this program, you will get the same set of numbers.

To get different numbers every time you can use: srand(unsigned int seed)
function; here seed is an unsigned integer. So you will need a different value of
seed every time you run the program for that you can use current time which

26 | P a g e S.Shah(Roll no 014) Discrete Structure Lab


will always be different so you will get a different set of numbers. By default,
seed = 1 if you do not use srand function.

Program:

#include <stdio.h>
#include <stdlib.h>
#include<time.h>

int main(void)

// This program will create different sequence of random numbers on every


program run Use current time as seed for random generator

srand(time(0));

int i;

printf("\n");

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

printf(" %d ", rand());

return 0;

Output

27 | P a g e S.Shah(Roll no 014) Discrete Structure Lab

You might also like