Slider

C Program to Convert Octal to Binary.

Given an octal number as input, write a C program to convert it to its equivalent binary representation. C program to convert an Octal to a Binary Nu
Given an octal number as input, write a C program to convert it to its equivalent binary representation.

Example:
Input: 52
Output: 101010

Input: 55
Output: 101101

  • Octal Number: An octal number is expressed in the base-8 numeral system, which uses the digits from 0 to 7.
  • Binary Number: A binary number is a number expressed in the base-2 numeral system, which uses only two symbols: 0 and 1.

C program to convert an Octal to a Binary Number.

//C code implementation to convert octal to binary
#include <stdio.h>

// Function to convert octal digit to binary
char* octalToBinary(char digit) {
    switch (digit) {
        case '0': return "000";
        case '1': return "001";
        case '2': return "010";
        case '3': return "011";
        case '4': return "100";
        case '5': return "101";
        case '6': return "110";
        case '7': return "111";
        default: return "";
    }
}

int main() {
    char octal[20];
    printf("Enter an octal number: ");
    scanf("%s", octal);

    // Convert octal to binary
    char binary[60] = "";
    for (int i = 0; octal[i] != '\0'; i++) {
        char* binaryDigit = octalToBinary(octal[i]);
        strcat(binary, binaryDigit);
    }

    printf("Binary Equivalent: %s", binary);

    return 0;
}
Output:
Enter an octal number: 63
Binary Equivalent: 110011

Time Complexity: O(n)
Space Complexity: O(n)

0

No comments

Post a Comment

both, mystorymag

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson
Table of Contents