JAWAHARLAL INSTITUTE OF TECHNOLOGY
VIDHYA VHIAR, BORAWAN, KHARGONE
MADHYA PRADESH (451001)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING.
NAME-: ANKIT VERMA
Enrollment No: 0805CS201016
SUBJECT-: LAB MANUAL THEROY OF
COMPUTATION (CS 501)
Name: Ankit Verma Enrollment No: 0805CS201016
LIST OF EXPERIMENTS
1. Design FA with ∑ = {0, 1} accepts the set of all
strings with three consecutive 0's.
2. Design a Program for creating machine that
accepts three consecutive one.
3. Program to DFA that accepts string ending with
01 or 10.
4. Write a Program for Mode using Counting
Sort technique.
5. Program to check if a Number is Divisible By
Second Number.
6. Program to Count the number of 1's and 0's in a
binary array.
7. Design a program for creating a machine which
count number of 1’s and 0’s in a given string.
8. Design a Program to find 2’s complement of a
given binary number.
9. Construct Turing Machine for incrementing
Binary Number by 1
10. Design a Turing machine that’s accepts the
following language a n b n c n where n>0
2.
Name: Ankit Verma Enrollment No: 0805CS201016
1.Design FA with ∑ = {0, 1} accepts the set of all strings with
three consecutive 0's.
The strings that will be generated for this particular languages are 000,
0001, 1000, 10001, .... in which 0 always appears in a clump of
3. The transition graph is as follows:
Design FA with ∑ = {0, 1} accepts even number of 0's and even number of
1's.
This FA will consider four different stages for input 0 and input 1. The
stages could be:
Here q0 is a start state and the final state also. Note carefully that a
symmetry of 0's and 1's is maintained. We can associate meanings to each
state as: q0: state of even number of 0's and even number of 1's.
q1: state of odd number of 0's and even number of 1's.
q2: state of odd number of 0's and odd number of 1's.
q3: state of even number of 0's and odd number of 1's.
3.
Name: Ankit Verma Enrollment No: 0805CS201016
2. Program to DFA that accepts string ending with 01 or 10.
#include <bits/stdc++.h>
using namespace std;
void q1(string, int);
void q2(string, int);
void q3(string, int);
void q4(string, int);
void q1(string s, int i)
cout << "q1->";
if (i == s.length()) {
cout << "NO \n";
return;
}if (s[i] == '0')
q1(s, i + 1);
else
q3(s, i + 1);
}void q2(string s, int i)
cout << "q2->";
4.
Name: Ankit Verma Enrollment No: 0805CS201016
if (i == s.length()) {
cout << "NO \n";
return;
}if (s[i] == '0')
q4(s, i + 1);
else
q2(s, i + 1);
}void q3(string s, int i)
cout << "q3->";
if (i == s.length()) {
cout << "YES \n";
return;
}if (s[i] == '0')
q4(s, i + 1);
elseq2(s, i + 1);
void q4(string s, int i)
cout << "q4->";
5.
Name: Ankit Verma Enrollment No: 0805CS201016
if (i == s.length()) {
cout << "YES \n";
return;
}if (s[i] == '0')
q1(s, i + 1);
else
q3(s, i + 1);
void q0(string s, int i)
cout << "q0->";
if (i == s.length()) {
cout << "NO \n";
return;
}if (s[i] == '0')
q1(s, i + 1);
else
q2(s, i + 1);
}int main()
6.
Name: Ankit Verma Enrollment No: 0805CS201016
string s = "010101";
cout << "State transitions are ";
q0(s, 0);
Output:
State transitions are q0->q1->q3->q4->q3->q4->q3->YES
3.C++ Program for Mode using Counting Sort technique.
7.
Name: Ankit Verma Enrollment No: 0805CS201016
#include <bits/stdc++.h>
using namespace std;
void printMode(int a[], int n)
int b[n]; // The output array b[] will
// have sorted array
int max = *max_element(a, a + n);
int t = max + 1;
int count[t];
for (int i = 0; i < t; i++)
count[i] = 0; // Store count of each element
for (int i = 0; i < n; i++)
count[a[i]]++;
// mode is the index with maximum count
int mode = 0;
int k = count[0];
for (int i = 1; i < t; i++) {
if (count[i] > k) {
k = count[i];
8.
Name: Ankit Verma Enrollment No: 0805CS201016
mode = i;
}}
cout << "mode = " << mode;
int main()
int a[] = { 1, 4, 1, 2, 7, 1, 2, 5, 3, 6 };
int n = sizeof(a) / sizeof(a[0]);
printMode(a, n);
return 0;
Output:
mode = 1
4.Program to Check if a Number is Divisible By Second Number
9.
Name: Ankit Verma Enrollment No: 0805CS201016
#include<iostream>
using namespace std;
int main()
{
int first, second;
cout << "Enter the numbers : ";
cin >> first >> second;
if (first % second == 0)
cout << "First number " << first
<< " is divisible by second number "
<< second;
else
cout << "First number " << first
<< " is not divisible by second number "
<< second;
}
Output:
Enter the numbers : 34 17
First number 34 is divisible by second number 17
Enter the numbers : 104 12
First number 104 is not divisible by second number 12
5.Design a program for creating a machine which accepts string
having equal no. of 1’s and 0’s.
10.
Name: Ankit Verma Enrollment No: 0805CS201016
#include <stdio.h>
int EE=0, OE=1, OO=2, EO=3; // DFA states
int state = 0; // initial DFA state
char input;
int main(void) {
printf("Enter a string of 0s and 1s: ");
while (1) {
scanf("%c", &input);
if (input == '') // if end-of-line exit loop
break;
if ( (input != '0') && (input != '1') ) { // invalid input printf("Invalid
input: program terminating");
break;
}
if (state==0) {
// input is either '0' or '1' state = (input == '0') ? OE : EO;
}
else if(state==1) {
state = (input == '0') ? EE : OO;
}
else if (state==2) {
state = (input == '0') ? EO : OE;
} else {
state = (input == '0') ? OO : EE;
11.
Name: Ankit Verma Enrollment No: 0805CS201016
}
};
if (input == '
') {
if (state == OO)
printf("Input accepted
");
else
printf("Input rejected
");
}
return 0;
}
Output:
Enter a string of 0s and 1s: 101010
Input accepted
Enter a string of 0s and 1s: 10011100
Input rejected
12.
Name: Ankit Verma Enrollment No: 0805CS201016
6. C++ program to Count the number of 1's and 0's in a binary
array
#include <bits/stdc++.h>
using namespace std;
// Function to check
// if bit is 1 or not
bool isOne(int i)
if (i == 1)
return true;
else
return false;
int main()
int a[] = { 1, 0, 0, 1, 0, 0, 1 };
int n = sizeof(a) / sizeof(a[0]);
int count_of_one = count_if(a, a + n, isOne);
cout << "1's: " << count_of_one << endl;
13.
Name: Ankit Verma Enrollment No: 0805CS201016
cout << "0's: " << (n - count_of_one) << endl;
return 0;
Output:
1's: 3
0's: 4
14.
Name: Ankit Verma Enrollment No: 0805CS201016
7. C++ program to Count the number of 1's and 0's in a binary
array
#include <bits/stdc++.h>
using namespace std;
// if bit is 1 or not
bool isOne(int i)
if (i == 1)
return true;
else
return false;
int main()
int a[] = { 1, 0, 0, 1, 0, 0, 1 };
int n = sizeof(a) / sizeof(a[0]);
int count_of_one = count_if(a, a + n, isOne);
cout << "1's: " << count_of_one << endl;
cout << "0's: " << (n - count_of_one) << endl;
15.
Name: Ankit Verma Enrollment No: 0805CS201016
return 0;
Output:
1's: 3
0's: 4
16.
Name: Ankit Verma Enrollment No: 0805CS201016
8. Design a Program to find 2’s complement of a given binary
number.
#include<iostream>
Using namespace std;
#define SIZE 8
int main()
{
int i, carry = 1;
char num[SIZE + 1], one[SIZE + 1], two[SIZE + 1];
cout<<"Enter the binary number"<<;
gets(num);
for(i = 0; i < SIZE; i++){
if(num[i] == '0'){
one[i] = '1';
}
else if(num[i] == '1'){
one[i] = '0';
} }
one[SIZE] = '\0';
cout<<"Ones' complement of binary number %s is
%s"<<num<<one;
for(i = SIZE - 1; i >= 0; i--)
{
if(one[i] == '1' && carry == 1){
two[i] = '0';
17.
Name: Ankit Verma Enrollment No: 0805CS201016
}
else if(one[i] == '0' && carry == 1){
two[i] = '1';
carry = 0;
}
else{
two[i] = one[i];
}}
two[SIZE] = '\0';
cout<<"Two's complement of binary number %s is %s"<<num<<
two;
return 0;
}
Output :
Enter the binary number
1000010
Ones' complement of binary number 1000010 is 0111101
Two's complement of binary number 1000010 is 0111110
18.
Name: Ankit Verma Enrollment No: 0805CS201016
9.Construct Turing Machine for incrementing Binary Number by 1.
Prerequisite : Turing Machine
Task :
We have to design a Turing Machine for incrementing the Binary Number
by 1.
Examples –
Input: 10111
Output: 11000
Input: 1000
Output: 1001
Input: 10101011
Output: 10101100
19.
Name: Ankit Verma Enrollment No: 0805CS201016
10. Design a Turing machine that’s accepts the following
language a n b n c n where n>0
Prerequisite – Turing Machine
The language L = {0n1n2n | n≥1} represents a kind of language where we
use only 3 character, i.e., 0, 1 and 2. In the beginning language has some
number of 0’s followed by equal number of 1’s and then followed by
equal number of 2’s. Any such string which falls in this category will be
accepted by this language. The beginning and end of string is marked by $
sign.
Examples –
Input : 0 0 1 1 2 2
Output : Accepted
Input : 0 0 0 1 1 1 2 2 2 2
Output : Not accepted
Assumption: We will replace 0 by X, 1 by Y and 2 by Z
Approach used –
First replace a 0 from front by X, then keep moving right till you find a 1
and replace this 1 by Y. Again, keep moving right till you find a 2, replace it
by Z and move left. Now keep moving left till you find a X. When you find
it, move a right, then follow the same procedure as above.
A condition comes when you find a X immediately followed by a Y. At this point
we keep moving right and keep on checking that all 1’s and 2’s have been
converted to Y and Z. If not then string is not accepted. If we reach $ then string
is accepted.
Step-1:
Replace 0 by X and move right, Go to state Q1.
Step-2:
Replace 0 by 0 and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
Replace 1 by Y and move right, go to state Q2.
Step-3:
Replace 1 by 1 and move right, Remain on same state
Replace Z by Z and move right, Remain on same state
Replace 2 by Z and move right, go to state Q3.
Step-4:
Replace 1 by 1 and move left, Remain on same state
Replace 0 by 0 and move left, Remain on same state
Replace Z by Z and move left, Remain on same state
20.
Name: Ankit Verma Enrollment No: 0805CS201016
Replace Y by Y and move left, Remain on same state
Replace X by X and move right, go to state Q0.
Step-5:
If symbol is Y replace it by Y and move right and Go to state Q4
Else go to step 1
Step-6:
Replace Z by Z and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO TO
FINAL STATE Q5
21.