C Program To Convert Octal to Binary Number

C program to convert Octal to binary number; Through this tutorial, we will learn how to convert octal numbers to binary numbers in c program using while loop and function.

C Program To Convert Octal to Binary Number

Let’s use the following programs to convert octal numbers to binary numbers in c program using while loop and function:

  • C Program to Convert Octal to Binary Number using While Loop
  • C Program to Convert Octal to Binary Number using Function

C Program to Convert Octal to Binary Number using While Loop

#include <stdio.h>
#include <math.h>

int main()
{
    int i, octal, decimal = 0;
    long binary = 0;
    i = 0;

    printf("Enter the Octal Number = ");
    scanf("%d",&octal);

    while(octal != 0)
    {
        decimal = decimal + (octal % 10) * pow(8, i);
        i++;
        octal = octal / 10;
    }
    i = 1;
    while(decimal != 0)
    {
        binary += ((decimal % 2) * i);
        decimal = decimal / 2;
        i = i * 10;
    }

    printf("The Binay Value = %ld\n", binary);
}

The output of the above c program; as follows:

Enter the Octal Number = 1025
The Binay Value = 1000010101

C Program to Convert Octal to Binary Number using Function

#include <stdio.h>
#include <math.h>

long octalToBinary(int octal)
{
    int i, decimal = 0;
    long binary = 0;
    for (i = 0; octal != 0; i++)
    {
        decimal = decimal + (octal % 10) * pow(8, i);
        octal = octal / 10;
    }

    for (i = 1; decimal != 0; i = i * 10)
    {
        binary = binary + (decimal % 2) * i;
        decimal = decimal / 2;
    }
    return binary;
}

int main()
{
    int octal;

    printf("Enter the Octal Number = ");
    scanf("%d", &octal);

    printf("The Decimal Value = %ld\n", octalToBinary(octal));
}

The output of the above c program; as follows:

Enter the Octal Number = 1256
The Decimal Value = 1010101110

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *