Program to Convert Octal to Binary
- Write a program to Convert Octal to Binary in C
- Write a program to Convert Octal to Binary in C++
- Write a program to Convert Octal to Binary in Python
- Write a program to Convert Octal to Binary in PHP
- Write a program to Convert Octal to Binary in Java
- Write a program to Convert Octal to Binary in Java Script
- Write a program to Convert Octal to Binary in C#
Explanation:
Each octal digit must be converted into its 3-bit binary counterpart in order to convert an octal number (base 8) to its binary equivalent (base 2). We can immediately transfer each octal digit to its binary equivalent since one octal digit equals exactly three binary digits (23 = 8).
Steps for Octal to Binary Conversion:
- Start with the octal number.
- For example: 345 (octal).
- Convert each octal digit to its 3-bit binary equivalent.
- Example: 3 → 011, 4 → 100, and 5 → 101.
- Combine the binary groups to get the final binary number.
- In this case, 345 in octal becomes 011100101 in binary.
Program to Convert Octal to Binary
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <math.h> int octalToDecimal(int octal) { int decimal = 0, base = 1; while (octal > 0) { int remainder = octal % 10; decimal += remainder * base; base *= 8; octal /= 10; } return decimal; } void decimalToBinary(int decimal) { int binary[32], i = 0; while (decimal > 0) { binary[i++] = decimal % 2; decimal /= 2; } for (int j = i - 1; j >= 0; j--) printf("%d", binary[j]); } int main() { int octal; printf("Enter an octal number: "); scanf("%d", &octal); int decimal = octalToDecimal(octal); printf("Binary: "); decimalToBinary(decimal); return 0; }
#include <iostream> #include <cmath> using namespace std; int octalToDecimal(int octal) { int decimal = 0, base = 1; while (octal > 0) { int remainder = octal % 10; decimal += remainder * base; base *= 8; octal /= 10; } return decimal; } void decimalToBinary(int decimal) { string binary = ""; while (decimal > 0) { binary = to_string(decimal % 2) + binary; decimal /= 2; } cout << binary; } int main() { int octal; cout << "Enter an octal number: "; cin >> octal; int decimal = octalToDecimal(octal); cout << "Binary: "; decimalToBinary(decimal); return 0; }
def octal_to_binary(octal): decimal = int(str(octal), 8) return bin(decimal).replace("0b", "") octal = input("Enter an octal number: ") print("Binary:", octal_to_binary(octal))
<?php function octalToBinary($octal) { $decimal = octdec($octal); return decbin($decimal); } $octal = readline("Enter an octal number: "); echo "Binary: " . octalToBinary($octal) . "\n"; ?>
import java.util.Scanner; public class OctalToBinary { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter an octal number: "); String octal = sc.nextLine(); int decimal = Integer.parseInt(octal, 8); String binary = Integer.toBinaryString(decimal); System.out.println("Binary: " + binary); } }
function octalToBinary(octal) { const decimal = parseInt(octal, 8); return decimal.toString(2); } const octal = prompt("Enter an octal number: "); console.log("Binary:", octalToBinary(octal));
using System; class OctalToBinary { static void Main() { Console.Write("Enter an octal number: "); string octal = Console.ReadLine(); int decimalValue = Convert.ToInt32(octal, 8); string binary = Convert.ToString(decimalValue, 2); Console.WriteLine("Binary: " + binary); } }