From 7e5c2f37e246e9a81a55a99c0ae88b85d6a82f3c Mon Sep 17 00:00:00 2001 From: Darakhshan Naheed <136336115+khandolly786@users.noreply.github.com> Date: Sat, 19 Oct 2024 11:17:35 +0530 Subject: [PATCH] Create BCD.cpp --- BCD.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 BCD.cpp diff --git a/BCD.cpp b/BCD.cpp new file mode 100644 index 0000000..b17028b --- /dev/null +++ b/BCD.cpp @@ -0,0 +1,22 @@ +#include +using namespace std; + +int main() { + int n; + cin >> n; + + string binary = ""; // Use a string to store the binary result + + while (n != 0) { + int bit = n & 1; // Get the least significant bit + binary = to_string(bit) + binary; // Append the bit to the left of the string + n = n >> 1; // Right shift the number to process the next bit + } + + if (binary == "") { + binary = "0"; // If the input number is 0, output "0" + } + + cout << binary; // Output the binary equivalent + return 0; +}