Program 8: Design a program which will increment the given Binary number by 1
Theory--:
Code :
#include <iostream>
#include <string>
using namespace std;
void q0(string, int);
void q1(string, int);
void q2(string, int);
int a[100], j = 0;
void q0(string s, int i)
if (i == [Link]()) {
cout << "\nBinary string after increment by 1: ";
for (int k = [Link]() - 1; k >= 0; k--)
cout << a[k];
cout << endl;
return;
cout << "\nq0 -> ";
if (s[i] == '0') {
cout << "Output is 1";
a[j++] = 1;
q1(s, i + 1);
} else {
cout << "Output is 0";
a[j++] = 0;
q0(s, i + 1);
}
void q1(string s, int i)
if (i == [Link]()) {
cout << "\nBinary string after increment by 1: ";
for (int k = [Link]() - 1; k >= 0; k--)
cout << a[k];
cout << endl;
return;
cout << "\nq1 -> ";
if (s[i] == '0') {
cout << "Output is 0";
a[j++] = 0;
q2(s, i + 1);
} else {
cout << "Output is 1";
a[j++] = 1;
q1(s, i + 1);
void q2(string s, int i)
if (i == [Link]()) {
cout << "\nBinary string after increment by 1: ";
for (int k = [Link]() - 1; k >= 0; k--)
cout << a[k];
cout << endl;
return;
cout << "\nq2 -> ";
if (s[i] == '0') {
cout << "Output is 0";
a[j++] = 0;
q2(s, i + 1);
} else {
cout << "Output is 1";
a[j++] = 1;
q1(s, i + 1);
int main()
string s;
cout << "Enter the binary string (0 or 1): ";
cin >> s;
string rev = string([Link](), [Link]());
cout << "\nThe state transitions are:";
q0(rev, 0);
return 0;
Output: