C Program To Convert Octal to Binary Number

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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