1.final DiscreteLabManual S Shah
1.final DiscreteLabManual S Shah
1.final DiscreteLabManual S Shah
Experiment
No. Title Remarks
Aim: Write a Program to calculate ab mod n for large a and b without using
power function.
Program:
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:
Output:
Result: ab mod n is calculated for large a and b without using power function.
Aim: To calculate GCD (Greatest Common Divisor) for large value numbers
say ‘a’ & ‘b’.
Program:
Result: GCD (Greatest Common Divisor) is calculated for large value numbers.
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++;
}
}
Output:
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 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;
}
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;
}
Output:
Aim: Write a program to construct DFA that accept a string ending at 00.
Program:
}
}
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);
}
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.
Aim: Write a program to construct DFA that accept a string starting at 0 and
ending at 1.
Program:
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.
Aim: Write a program to identify whether the expression entered has correct
number of parentheses.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 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();
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();
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();
}
printf("\n\nMatrix is reflexive");
getch();
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
Program:
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main(void)
srand(time(0));
int i;
printf("\n");
return 0;
Output