COAL Lab Task
COAL Lab Task
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>
return decimal;
}
// Function to convert decimal to binary
string decimalToBinary(int decimal) {
string binary = "";
return binary;
}
return decimalToBinary(sum);
}
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;
}