BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Convert Octal Number to Binary Number

By Chaitanya Singh | Filed Under: C Programs

This program converts an octal number to a binary number using a user defined function.

Example: Program to convert octal to binary

In this program we have created a user defined function octalToBinary(). This function converts the octal number (entered by user) to decimal number first and then converts that decimal number to binary number. To understand the working of this program, you should have the basic knowledge of following C Programming topics:

  1. C Programming while loop
  2. C Programming functions
#include <stdio.h>
#include <math.h>
//This function converts octal number to binary number
long octalToBinary(int octalnum)
{
    int decimalnum = 0, i = 0;
    long binarynum = 0;

    /* This loop converts octal number "octalnum" to the
     * decimal number "decimalnum"
     */
    while(octalnum != 0)
    {
	decimalnum = decimalnum + (octalnum%10) * pow(8,i);
	i++;
	octalnum = octalnum / 10;
    }

    //i is re-initialized
    i = 1;

    /* This loop converts the decimal number "decimalnum" to the binary
     * number "binarynum"
     */
    while (decimalnum != 0)
    {
	binarynum = binarynum + (decimalnum % 2) * i;
	decimalnum = decimalnum / 2;
	i = i * 10;
    }

    //Returning the binary number that we got from octal number
    return binarynum;
}
int main()
{
    int octalnum;

    printf("Enter an octal number: ");
    scanf("%d", &octalnum);

    //Calling the function octaltoBinary
    printf("Equivalent binary number is: %ld", octalToBinary(octalnum));

    return 0;
}

Output:

Enter an octal number: 71
Equivalent binary number is: 111001

Check out these related C Programs:

  1. C Program to convert Binary number to Octal number
  2. C Program to convert binary to decimal
  3. C Program to convert decimal to an Octal number
  4. C Program to convert lowercase string to uppercase string
  5. C Program to convert uppercase string to lowercase

Leave a Reply Cancel reply

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

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap