Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- std::string transform(const std::string &binaryStr) {
- std::string result;
- for (char bit : binaryStr) {
- if (bit == '0') {
- result += "01";
- } else if (bit == '1') {
- result += "10";
- }
- }
- return result;
- }
- std::string toBinary(int num) {
- std::string binaryStr;
- while (num > 0) {
- binaryStr = (num % 2 ? '1' : '0') + binaryStr;
- num /= 2;
- }
- return binaryStr.empty() ? "0" : binaryStr;
- }
- int binaryToDecimal(const std::string &binaryStr) {
- int decimalValue = 0;
- int base = 1;
- for (int i = binaryStr.size() - 1; i >= 0; --i) {
- if (binaryStr[i] == '1') {
- decimalValue += base;
- }
- base *= 2;
- }
- return decimalValue;
- }
- int main() {
- const int maxNumber = 44;
- std::cout << std::left << std::setw(5) << "Odd Number"
- << std::setw(20) << "Binary"
- << std::setw(30) << "Transformed Binary"
- << "Decimal Value\n";
- std::cout << std::string(75, '-') << '\n';
- for (int i = 1; i <= maxNumber; i += 2) {
- std::string binaryStr = toBinary(i);
- std::string transformedBinaryStr = transform(binaryStr);
- int decimalValue = binaryToDecimal(transformedBinaryStr);
- std::cout << std::left << std::setw(15) << i
- << std::setw(20) << binaryStr
- << std::setw(30) << transformedBinaryStr
- << decimalValue << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment