0% found this document useful (0 votes)
10 views5 pages

Increment Binary Number by One

The document describes a C++ program designed to increment a given binary number by 1. It includes functions for handling different states during the increment process and outputs the resulting binary string. The program takes user input for a binary string and processes it through recursive functions to achieve the increment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Increment Binary Number by One

The document describes a C++ program designed to increment a given binary number by 1. It includes functions for handling different states during the increment process and outputs the resulting binary string. The program takes user input for a binary string and processes it through recursive functions to achieve the increment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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:

You might also like