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

COAL Lab Task

Create a c++ program that can perform the following tasks: 1) Binary to decimal conversion. 2) Decimal to binary conversion. 3) Binary Addition. 4) Binary Subtraction.

Uploaded by

233053
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 views8 pages

COAL Lab Task

Create a c++ program that can perform the following tasks: 1) Binary to decimal conversion. 2) Decimal to binary conversion. 3) Binary Addition. 4) Binary Subtraction.

Uploaded by

233053
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/ 8

Name : M.

Asad Naveed
Roll Number : 233053
Section : BSCYS “eve A”
Subject : COAL (Computer Organization and Assembly Language)

QUESTION :1
Create a c++ program that can perform the following tasks:
1) Binary to decimal conversion.
2) Decimal to binary conversion.
3) Binary Addition.
4) Binary Subtraction.

Solution :

#include <iostream>
#include <string>
#include <stdexcept>

using namespace std;

// Function to convert binary to decimal


int binaryToDecimal(string binary) {
int decimal = 0;
int base = 1;

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


if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}

return decimal;
}
// Function to convert decimal to binary
string decimalToBinary(int decimal) {
string binary = "";

while (decimal > 0) {


binary = (decimal % 2 == 0 ? "0" : "1") + binary;
decimal /= 2;
}

return binary;
}

// Function to perform binary addition


string binaryAddition(string binary1, string binary2) {
int decimal1 = binaryToDecimal(binary1);
int decimal2 = binaryToDecimal(binary2);
int sum = decimal1 + decimal2;

return decimalToBinary(sum);
}

// Function to perform binary subtraction


string binarySubtraction(string binary1, string binary2) {
int decimal1 = binaryToDecimal(binary1);
int decimal2 = binaryToDecimal(binary2);
int difference = decimal1 - decimal2;

if (difference < 0) {
throw runtime_error("Error: Binary subtraction result is negative.");
}

return decimalToBinary(difference);
}

int main() {
int choice;

while (true) {
cout << "Binary Converter and Calculator Menu" << endl;
cout << "-----------------------------------" << endl;
cout << "1. Binary to Decimal Conversion" << endl;
cout << "2. Decimal to Binary Conversion" << endl;
cout << "3. Binary Addition" << endl;
cout << "4. Binary Subtraction" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
string binary;
cout << "Enter a binary number: ";
cin >> binary;
int decimal = binaryToDecimal(binary);
cout << "The decimal equivalent is: " << decimal << endl;
break;
}
case 2: {
int decimal;
cout << "Enter a decimal number: ";
cin >> decimal;
string binary = decimalToBinary(decimal);
cout << "The binary equivalent is: " << binary << endl;
break;
}
case 3: {
string binary1, binary2;
cout << "Enter the first binary number: ";
cin >> binary1;
cout << "Enter the second binary number: ";
cin >> binary2;
string sum = binaryAddition(binary1, binary2);
cout << "The sum is: " << sum << endl;
break;
}
case 4: {
string binary1, binary2;
cout << "Enter the first binary number: ";
cin >> binary1;
cout << "Enter the second binary number: ";
cin >> binary2;
try {
string difference = binarySubtraction(binary1, binary2);
cout << "The difference is: " << difference << endl;
} catch (const exception& e) {
cerr << e.what() << endl;
}
break;
}

😄😄😄
case 5:
cout << "Exiting the program. " << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}

return 0;
}

Working Screenshots of Code:


1)Binary to decimal :
2)Decimal to binary:
3)Binary Addition
4)Binary Subtraction

You might also like