0% found this document useful (0 votes)
7 views32 pages

theoryofcomputationassinment(0902CS211008)

The document outlines a series of programming experiments related to the Theory of Computation for a BTech Computer Science course. Each experiment includes objectives, code implementations, and expected outputs for various computational tasks such as accepting binary strings, designing machines, and implementing algorithms. The document serves as a practical guide for students to apply theoretical concepts in programming.

Uploaded by

cse211009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

theoryofcomputationassinment(0902CS211008)

The document outlines a series of programming experiments related to the Theory of Computation for a BTech Computer Science course. Each experiment includes objectives, code implementations, and expected outputs for various computational tasks such as accepting binary strings, designing machines, and implementing algorithms. The document serves as a practical guide for students to apply theoretical concepts in programming.

Uploaded by

cse211009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

RUSTAMJI INSTITUTE OF TECHNOLOGY

BSF ACADEMY TEKANPUR


THEORY OF COMPUTATION (CS501)

BTECH COMPUTER SCIENCE & ENGINEERING 5 th SEMESTER

(2021-2025)

SUBMITTED BY
Amit Sharma
(0902CS211008)

SUBMITTED TO
PROF. YOGRAJ SHARMA
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INDEX

S.No Content PageNo.

1. DESIGN A PROGRAM FOR CREATING A MACHINE 03


THAT WILL ACCEPT ANY BINARY STRING WITH
THREE CONSECUTIVE 1’S

2. DESIGN A PROGRAM FOR CREATING A MACHINE 06


THAT WILL ACCEPT ANY BINARY STRING
ENDING WITH 101

3. DESIGN A PROGRAM FOR MOD 3 MACHINE 09

4. DESIGN A PROGRAM THAT WILL ACCEPT 12


DECIMAL NUMBERS DIVISIBLE BY 2.

5. DESIGN A PROGRAM THAT ACCEPTS STRINGS 15


HAVING EQUAL NUMBER OF ONES AND ZEROES

6. DESIGN A PROGRAM THAT COUNT NUMBER OF 18


ONES AND ZEROES IN A GIVEN STRING

7. DESIGN A PROGRAM THAT RETURNS TWO’S 21


COMPLEMENT OF A GIVEN BINARY NUMBER

8. DESIGN A PROGRAM THAT INCREMENTS A 24


BINARY NUMBER BY ONE

9. DESIGN A PDA TO ACCEPT WCWR WHERE W IS 27


ANY STRING AND WR IS REVERSE OF THAT
STRING W AND C IS A SPECIAL SYMBOL

10. DESIGN A TURING MACHINE THAT ACCEPTS 30


THE FOLLOWING LANGUAGE L={a^nb^nc^n}
OVER ALPHABET a,b,c WHERE n>0

Amit Sharma (0902CS211008) Page 2


EXPERIMENT NO: 1

OBJECTIVE: DESIGN A PROGRAM FOR CREATING A MACHINE THAT


WILL ACCEPT ANY STRING WITH THREE CONSECUTIVE 1’S

CODE :
#include <iostream>
using namespace std;
int main()
{
string a;
int i = 0, b;
bool check = 0;
while (!check)
{
cout << "enter the string ";
cin >> a;
b = a.length();
for (i = 0; i < b; i++)
{
if (a[i] == '1' || a[i] == '0')
{
check = 1;
}
else
{
check = 0;

break;
}

Amit Sharma (0902CS211008) Page 3


}

if (check == 1)
{

bool check2;
for (i = 0; i < b; i++)
{
if (a[i] == '1' && a[i+1] == '1' && a[i+2] == '1')
{
check2 = 1;
}
}
if (check2 == 1)
{
cout << "STRING ACCEPTED" << endl;
}
else
{
cout << "STRING REJECTED" << endl;
}
}
else
{
cout << "invalid string" << endl;
cout<<"please enter a valid string"<<endl;
}
}
}

Amit Sharma (0902CS211008) Page 4


OUTPUT:

Amit Sharma (0902CS211008) Page 5


EXPERIMENT NO: 2

OBJECTIVE : DESIGN A PROGRAM FOR CREATING A MACHINE


THAT WILL ACCEPT ANY BINARY STRING ENDING WITH 101

CODE :
#include <iostream>
using namespace std;
int main()
{
string a;
int i = 0, b;
bool check = 0;
while (!check)
{
cout << "enter the string ";
cin >> a;
b = a.length();
for (i = 0; i < b; i++)
{
if (a[i] == '1' || a[i] == '0')
{
check = 1;
}
else
{
check = 0;

break;

Amit Sharma (0902CS211008) Page 6


}
}

if (check == 1)
{

bool check2;
{
if (a[b-3] == '1' && a[b-2] == '0' && a[b-1] == '1')
{
check2 = 1;
}
}
if (check2 == 1)
{
cout << "STRING ACCEPTED" << endl;
}
else
{
cout << "STRING REJECTED" << endl;
}
}
else
{
cout << "invalid string" << endl;
cout<<"please enter a valid string"<<endl;
}
}
}

Amit Sharma (0902CS211008) Page 7


OUTPUT:

Amit Sharma (0902CS211008) Page 8


EXPERIMENT NO: 3

OBJECTIVE : DESIGN A PROGRAM FOR MOD 3 MACHINE

CODE :
#include <bits/stdc++.h>

using namespace std;

int finddecimalvalue(string s)

{ int a=0;

int ans=0;

int length=s.length();

for(int i=length-1;i>=0;i--)

{ int b=int(s[i]-48);

b=b*pow(2,a);

ans=ans+b;

a++;

}return ans;

bool chkvalid(string s)

{ int decimalvalue=finddecimalvalue(s);

if(decimalvalue%3)

return 0;

return 1;

int main()

Amit Sharma (0902CS211008) Page 9


{ bool isbinary = 0, isvalid = 0;

while(!isbinary)

{ string s;

cout<<"enter binary string:";

cin>>s;

isbinary=1;

for(int i=0;i<s.length();i++)

{ if(s[i]!='1' and s[i]!='0')

isbinary=0;

if(isbinary==1)

{ isvalid=chkvalid(s);

if(isvalid)

cout<<"STRING ACCEPTED"<<endl;

else

cout<<"STRING REJECTED"<<endl;

else{

isbinary=0;

cout<<"Invalid string"<<endl;

cout<<"Please enter a valid string"<<endl;

return 0;

Amit Sharma (0902CS211008) Page 10


OUTPUT:

Amit Sharma (0902CS211008) Page 11


EXPERIMENT NO: 4

OBJECTIVE : DESIGN A PROGRAM THAT WILL ACCEPT DECIMAL


NUMBERS DIVISIBLE BY 2.

CODE:
#include <bits/stdc++.h>

using namespace std;

bool ckhvalid(string s)

if (s[s.length() - 1] == '0')

return 1;

return 0;

int main()

bool ckhbinary = 0;

bool chkvalid = 0;

while (!ckhbinary)

{ string s;

cout << "enter binary string:";

cin >> s;

ckhbinary = 1;

for (int i = 0; i < s.length(); i++)

Amit Sharma (0902CS211008) Page 12


if (s[i] != '1' and s[i] != '0')

ckhbinary = 0;

if (ckhbinary == 1)

chkvalid = ckhvalid(s);

if (chkvalid)

cout << "STRING ACCEPTED" << endl;

else

cout << "STRING REJECTED" << endl;

else

{ ckhbinary = 0;

cout << "Invalid string" << endl;

cout << "Please enter a valid string.." << endl;

return 0;

OUTPUT:

Amit Sharma (0902CS211008) Page 13


Amit Sharma (0902CS211008) Page 14
EXPERIMENT NO: 5

OBJECTIVE : DESIGN A PROGRAM THAT ACCEPTS BINARY


STRINGS HAVING EQUAL NUMBER OF ONES AND ZEROES .
CODE:
#include <iostream>

using namespace std;

int main()

string a;

int i, b=0;

int count1=0, count0=0;

bool check;

while( !check)

cout<<"enter a string "<<endl;

cin>>a;

b=a.length();

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

if(a[i]=='1'||a[i]=='0')

check=1;

else

Amit Sharma (0902CS211008) Page 15


{

check=0;

break;

if(check==1)

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

if(a[i]=='1')

count1=count1+1;

else

count0=count0+1;

if(count1==count0)

cout<<"string accepted "<<endl;

else

cout<<"string rejected "<<endl;

Amit Sharma (0902CS211008) Page 16


}

else

cout<<"invalid string "<<endl;

cout<<"please enter a valid string "<<endl;

OUTPUT:

EXPERIMENT NO: 6

Amit Sharma (0902CS211008) Page 17


OBJECTIVE: DESIGN A PROGRAM THAT COUNT NUMBER OF
ONES AND ZEROES IN A GIVEN STRING.

CODE:
#include <iostream>

using namespace std;

int main()

string a;

int i, b=0;

int count1=0, count0=0;

bool check;

while( !check)

cout<<"enter a string "<<endl;

cin>>a;

b=a.length();

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

if(a[i]=='1'||a[i]=='0')

check=1;

else

Amit Sharma (0902CS211008) Page 18


{

check=0;

if(check==1)

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

if(a[i]=='1')

count1=count1+1;

else

count0=count0+1;

cout<<"the no of 1's in the given string are "<<count1<<endl;

cout<<"the no of 0's in the given string are "<<count0<<endl;

else

cout<<"invalid string "<<endl;

Amit Sharma (0902CS211008) Page 19


cout<<"please enter a valid string "<<endl;

OUTPUT:

Amit Sharma (0902CS211008) Page 20


EXPERIMENT NO: 7

OBJECTIVE: DESIGN A PROGRAM THAT RETURNS TWO’S


COMPLEMENT OF A GIVEN BINARY NUMBER.

CODE:
#include <iostream>

using namespace std;

int main()

string binary;

int i, b=0;

bool check;

while( !check)

cout<<"enter a binary string "<<endl;

cin>>binary;

int n=binary.length();

bool foundOne = false;

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

if(binary[i]=='1'||binary[i]=='0')
Amit Sharma (0902CS211008) Page 21
{

check=1;

else

check=0;

break;

if(check==1)

for (int i = n - 1; i >= 0; i--) {

if (binary[i] == '1' && !foundOne) {

foundOne = true;

continue;

if (foundOne) {

binary[i] = (binary[i] == '0') ? '1' : '0';

cout << "Two's complement: " << binary << endl;

Amit Sharma (0902CS211008) Page 22


else

cout<<"invalid string "<<endl;

cout<<"please enter a valid string "<<endl;

OUTPUT:

EXPERIMENT NO: 8

Amit Sharma (0902CS211008) Page 23


OBJECTIVE: DESIGN A PROGRAM THAT INCREMENTS A BINARY
NUMBER BY ONE

CODE:
#include <iostream>

using namespace std;

int main()

string binary;

int i, b=0;

bool check;

while( !check)

cout<<"enter a binary string "<<endl;

cin>>binary;

int n=binary.length();

int carry = 1;

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

if(binary[i]=='1'||binary[i]=='0')

check=1;

else

Amit Sharma (0902CS211008) Page 24


{

check=0;

break;

if(check==1)

for (int i = n - 1; i >= 0; i--) {

if (carry == 0) break;

if (binary[i] == '0') {

binary[i] = '1';

carry = 0;

} else {

binary[i] = '0';

if (carry == 1) {

binary = '1' + binary;

cout << "Incremented binary number: " << binary << endl;

else

Amit Sharma (0902CS211008) Page 25


cout<<"invalid string "<<endl;

cout<<"please enter a valid string "<<endl;

OUTPUT:

Amit Sharma (0902CS211008) Page 26


EXPERIMENT NO: 9

OBJECTIVE: DESIGN A PDA TO ACCEPT WCWR WHERE W IS ANY


STRING AND WR IS THE REVERSE OF THAT STRING W AND C IS A
SPECIAL SYMBOL

CODE:
#include <iostream>

#include <string>

#include <algorithm>

using namespace std;

bool isvalid(const string &input, char speccialcharacter)

int length = input.length();

int middle = length / 2;

if (length % 2 != 1 or input[middle] != speccialcharacter)

return 0;

string w = input.substr(0, middle);

string v = input.substr(middle + 1);

string vreversed = v;

reverse(vreversed.begin(), vreversed.end());

return w == vreversed;

int main()

Amit Sharma (0902CS211008) Page 27


{

bool flag = 1;

while (flag)

string number;

cout << "Enter the string:";

cin >> number;

char speccialcharacter;

cout << "Enter the special symbol";

cin >> speccialcharacter;

if (isvalid(number, speccialcharacter))

cout << "Given string is valid!!" << endl;

else

cout << "Given string is not valid!!" << endl;

string stop;

cout << "Do you want to continue?(yes/no): ";

cin >> stop;

if (stop != "yes")

flag = 0;

return 0;

Amit Sharma (0902CS211008) Page 28


OUTPUT:

Amit Sharma (0902CS211008) Page 29


EXPERIMENT NO: 10

OBJECTIVE: DESIGN A TURING MACHINE THAT ACCEPTS THE


FOLLOWING LANGUAGE L={a^nb^nc^n} OVER ALPHABET a,b,c
WHERE n>0.

CODE:
#include <iostream>

using namespace std;

bool acceptable(string s)

{ int n = s.length();

int counta = 0, countb = 0, countc = 0;

bool valid = 1;

for (char ch : s)

{ if (ch == 'a')

{ counta++;

if (countb > 0 or countc > 0)

return 0;

else if (ch == 'b')

{ countb++;

if (countc > 0 or counta == 0)

return 0;

Amit Sharma (0902CS211008) Page 30


else if (ch == 'c')

{ countc++;

if (counta == 0 or countb == 0)

return 0;

return ((counta == countb) and (countb == countc));

int main()

{ bool check = 1;

while (check)

{ string s;

cout << "Enter the string: ";

cin >> s;

bool accept = acceptable(s);

if (accept) cout << "STRING ACCEPTED!!" << endl;

else cout << "STRING REJECTED" << endl;

string stop;

cout << "Do you want to continue?(yes/no): ";

cin >> stop;

if (stop != "yes") check = 0;

return 0;

Amit Sharma (0902CS211008) Page 31


OUTPUT:

Amit Sharma (0902CS211008) Page 32

You might also like