×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to convert an octal number into a binary number

Here, we are going to learn how to convert an octal number into a binary number in C programming language?
Submitted by Nidhi, on July 03, 2021

Problem statement

Here, we will read an octal number and convert it into a binary number and print the result on the console screen.

C program to convert an octal number into a binary number

The source code to convert an octal number into a binary number is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to convert an octal number 
// into binary number

#include <stdio.h>

int main()
{
    char octalNum[32];
    int i = 0;

    printf("Enter octal number: ");
    scanf("%s", octalNum);

    printf("Binay number: ");
    while (octalNum[i]) {
        switch (octalNum[i]) {
        case '0':
            printf("000");
            break;

        case '1':
            printf("001");
            break;

        case '2':
            printf("010");
            break;

        case '3':
            printf("011");
            break;

        case '4':
            printf("100");
            break;

        case '5':
            printf("101");
            break;

        case '6':
            printf("110");
            break;

        case '7':
            printf("111");
            break;

        default:
            printf("\nInvalid octal number");
            return 0;
        }
        i++;
    }

    return 0;
}

Output

Enter octal number: 123
Binay number: 001010011

Explanation

Here, we created a character array octalNum and integer variable i. Then we read the octal number and convert the number into a binary number and printed the result on the console screen.

C Number System Conversion Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.