From ca2b69a63af99b2a66b2e693b9e7c16c6947fd95 Mon Sep 17 00:00:00 2001 From: Aakrisht69 <115407142+Aakrisht69@users.noreply.github.com> Date: Thu, 20 Oct 2022 00:25:34 +0530 Subject: [PATCH] Create OctalToBinary.cpp Hacktoberfest Submission. #13 --- Cpp/OctalToBinary.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Cpp/OctalToBinary.cpp diff --git a/Cpp/OctalToBinary.cpp b/Cpp/OctalToBinary.cpp new file mode 100644 index 0000000..3c04efd --- /dev/null +++ b/Cpp/OctalToBinary.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +// Function to convert an +// Octal to Binary Number +string OctToBin(string octnum) +{ + long int i = 0; + + string binary = ""; + + while (octnum[i]) { + switch (octnum[i]) { + case '0': + binary += "000"; + break; + case '1': + binary += "001"; + break; + case '2': + binary += "010"; + break; + case '3': + binary += "011"; + break; + case '4': + binary += "100"; + break; + case '5': + binary += "101"; + break; + case '6': + binary += "110"; + break; + case '7': + binary += "111"; + break; + default: + cout << "\nInvalid Octal Digit " + << octnum[i]; + break; + } + i++; + } + + return binary; +} + +// Driver code +int main() +{ + // Get the Hexadecimal number + string octnum = "345"; + + // Convert Octal to Binary + cout << "Equivalent Binary Value = " + << OctToBin(octnum); + + return 0; +}