Coin Toss Simulator code
#include
#include
#include
#include
using namespace std;
// Convert decimal to binary, octal, hexadecimal
void convertFromDecimal(int decimal) {
cout << "Binary: " << bitset<32>(decimal) << endl;
cout << "Octal: " << oct << decimal << endl;
cout << "Hexadecimal: " << hex << uppercase << decimal << endl;
}
// Convert binary string to decimal
int binaryToDecimal(const string& binary) {
return stoi(binary, nullptr, 2);
}
// Convert octal string to decimal
int octalToDecimal(const string& octal) {
return stoi(octal, nullptr, 8);
}
// Convert hex string to decimal
int hexToDecimal(const string& hex) {
int value;
stringstream ss;
ss << hex << hex;
ss >> std::hex >> value;
return value;
}
int main() {
int choice;
string input;
int decimal;
while (true) {
cout << "\n--- Base Converter ---\n";
cout << "1. Convert Decimal to Binary/Octal/Hex\n";
cout << "2. Convert Binary to Decimal\n";
cout << "3. Convert Octal to Decimal\n";
cout << "4. Convert Hex to Decimal\n";
cout << "5. Exit\n";
cout << "Choose an option: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter decimal number: ";
cin >> decimal;
convertFromDecimal(decimal);
break;
case 2:
cout << "Enter binary number: ";
cin >> input;
try {
cout << "Decimal: " << binaryToDecimal(input) << endl;
} catch (...) {
cout << "Invalid binary input.\n";
}
break;
case 3:
cout << "Enter octal number: ";
cin >> input;
try {
cout << "Decimal: " << octalToDecimal(input) << endl;
} catch (...) {
cout << "Invalid octal input.\n";
}
break;
case 4:
cout << "Enter hexadecimal number: ";
cin >> input;
try {
cout << "Decimal: " << hexToDecimal(input) << endl;
} catch (...) {
cout << "Invalid hexadecimal input.\n";
}
break;
case 5:
cout << "Exiting Base Converter.\n";
return 0;
default:
cout << "Invalid option. Try again.\n";
}
}
return 0;
}
Code output
--- Base Converter ---
1. Convert Decimal to Binary/Octal/Hex
2. Convert Binary to Decimal
3. Convert Octal to Decimal
4. Convert Hex to Decimal
5. Exit
Choose an option: 1
Enter decimal number: 42
Binary: 00000000000000000000000000101010
Octal: 52
Hexadecimal: 2A