- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- C Add Subtract Multiply Divide
- C Add n Numbers
- C Area Perimeter of Square
- C Area Perimeter of Rectangle
- C Area Circum of Circle
- C Fahrenheit to Celsius
- C Celsius to Fahrenheit
- C Inches to Centimeters
- C Kilogram to Gram
- C Reverse a Number
- C Swap Two Numbers
- C Interchange Numbers
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Find Armstrong Numbers
- C Find nCr and nPr
- C Find Profit Loss
- C Sum of their Square
- C First & Last Digit Sum
- C Sum of All Digit
- C Product of All Digit
- C Print Total Digit in Number
- C Check Perfect Number
- C Find Basic Gross Salary
- C Round Number to Integer
- C Print Series upto n Term
- C Find Factors of Number
- C if-else & Loop Programs
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Is Reverse Equal Original
- C Make Calculator
- C Add Digits of Number
- Count Positive Negative Zero
- C Largest of Two Numbers
- C Largest of Three Numbers
- C Smallest of Two Numbers
- C Smallest of Three Numbers
- C Find Factorial of Number
- C Find LCM & HCF
- C Find LCM of n Numbers
- C Find HCF of n Numbers
- C Find Arithmetic Mean
- C Find Average, Percentage
- C Find Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Find Discount Purchase
- C Calculate Parcel Charge
- C Calculate Wage of Labor
- C Print Phone Bill
- C Conversion programs
- C Decimal to Binary
- C Decimal to Octal
- C Decimal to Hexadecimal
- C Binary to Decimal
- C Binary to Octal
- C Binary to Hexadecimal
- C Octal to Decimal
- C Octal to Binary
- C Octal to Hexadecimal
- C Hexadecimal to Decimal
- C Hexadecimal to Binary
- C Hexadecimal to Octal
- C Pattern Programs
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Array Programs
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Largest Element in Array
- C Smallest Element in Array
- C Second Largest/Smallest
- C Count Even Odd
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Sum/Product of Even/Odd
- C Reverse an Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge Two Arrays
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Print Common Elements
- C 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C Sum All Matrix Elements
- C Largest Element in Matrix
- C Print Row Column Total
- C 3D Array Programs
- C String Programs
- C Print String
- C Find Length of String
- C Compare Two String
- C Copy a String
- C Concatenate String
- C Reverse a String
- C Count Vowels Consonants
- C Replace Vowel in String
- C Delete Vowels from String
- C Delete Word from String
- C Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Sort String in Alphabetical
- C Sort Words in Ascending
- C Sort Words in Descending
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Check Palindrome String
- C Print Number in Words
- C Print Successive Character
- C Character without Space
- C Remove Extra Spaces
- C File Programs
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C Reverse File
- C Count All Character in File
- C List Files in Directory
- C Encrypt & Decrypt a File
- C Delete a File
- C Misc Programs
- Generate Random Numbers
- C Print Date Time
- C Print Message with Time
- C Get IP Address
- C Print Smiling face
- C Pass Array to Function
- Add Two Numbers using Pointer
- C Address of Variable
- C Shutdown Computer
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

# C Program to Convert Octal to Binary

In this tutorial, we will learn about how to create a program in C that converts any given octal number by user
at run-time into its equivalent binary value. At last we have also created a function-driven program that does the
same job. In function-driven program, we have defined a function named
**OctToBin()** that takes octal number as argument and converts it into its equivalent binary value.

But before going through the program, if you are not aware of

- Octal Number
- Binary Number
- Octal to Binary Conversion Process

then refer to Octal to Binary conversion step by step process. Now let's move on to the program.

## Octal to Binary in C

To convert octal number to binary number in C programming, you have to ask from user to enter octal value and convert it into binary
value, then display the equivalent value in binary as output. The question is, **Write a program in C that converts any given number
(in octal number system) to its equivalent value in binary number system**. The answer to this question is:

#include<stdio.h> #include<conio.h> int main() { int octnum, rev=0, rem; printf("Enter any Octal Number: "); scanf("%d", &octnum); while(octnum!=0) { rem = octnum%10; rev = (rev*10) + rem; octnum = octnum/10; } octnum = rev; printf("\nEquivalent Binary value = "); while(octnum!=0) { rem = octnum%10; switch(rem) { 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(" InvalidOctalDigit(%d) ", rem); break; } octnum = octnum/10; } getch(); return 0; }

As the above program was written under **Code::Blocks** IDE, therefore after successful build and run, here is the sample run.
This is the first snapshot:

Now supply any number say **734** as input and press ENTER key to see its value in Binary form as shown in the second snapshot
of the sample run give here:

As the number **734** has 3 digits, that are **7**, **3** and **4**. Here 7 in binary is 111, 3 in binary is
011 and 4 in binary is 100. Therefore after combining all numbers in octal and binary, we have **734** (in octal) equals
**111011100** (in binary) as shown in above snapshot. You can also write **(734) _{8}** =

**(111011100)**

_{2}#### Program Explained

- Receive any number (in octal) as input say
**734** - Now reverse the number using while loop
- Here we have reverse the octal number because while calculating remainder one by one, of given octal number, we will get last
digit as first remainder and first digit as last remainder. For example, using modulous (%)
operator to find digit or remainder of
any number say
**734**. That is using**734%10**, we will get**4**as first digit or remainder. And here 4 is the last digit of the number**734**. Therefore we have reverse the number first - Now initialize the reverse of the number stored in
**rev**variable to the**octnum**variable - Create a
**while**loop that runs until the value of**octnum**becomes 0 - At first run, the condition of the
**while**loop,**octnum!=0**or**437!=0**(as we have reverse the number 734) evaluates to be true, therefore program flow goes inside the loop - And
**octnum%10**or**437%10**or**7**(first digit of**734**) gets initialized to**rem** - Create
**switch()**case, to check what the value present inside**rem**variable - Whatever the value present in
**rem**variable, print its equivalent binary value. For example, if**rem**holds 7, then binary equivalent of 7 gets printed as output, and after printing binary equivalent of**rem**,**octnum/10**or**437/10**or**43**gets initialized to**octnum**and the program flow goes back to the condition of the**while**loop - There, the condition
**octnum!=0**or**43!=0**evaluates to be true again, therefore program flow goes inside the loop - And
**octnum%10**or**43%10**or**3**gets initialized to**rem**, and then using**switch()**case, binary equivalent of**3**gets printed as output, and continue to do the similar operation as told above, until the value of**octnum**becomes 0 - In this way, binary equivalent of given octal number gets printed on output screen

### Octal to Binary in C using strcat() Function

Let's create the same purpose program in another way. Here we have used the string function named **strcat()** of **string.h**
library, to concatenate the string one by one. We have stored binary value in string format as shown in the program given below. The
question is, **Write a program in C that converts octal number to its equivalent binary number using library function strcat().**
The answer to this question is:

#include<stdio.h> #include<conio.h> #include<string.h> int main() { int octnum, rev=0, rem, count=0; char binnum[40] = ""; printf("Enter any Octal Number: "); scanf("%d", &octnum); while(octnum!=0) { rem = octnum%10; if(rem>7) { count++; break; } rev = (rev*10) + rem; octnum = octnum/10; } if(count==0) { octnum = rev; printf("\nEquivalent Binary value = "); while(octnum!=0) { rem = octnum%10; switch(rem) { case 0: strcat(binnum, "000"); break; case 1: strcat(binnum, "001"); break; case 2: strcat(binnum, "010"); break; case 3: strcat(binnum, "011"); break; case 4: strcat(binnum, "100"); break; case 5: strcat(binnum, "101"); break; case 6: strcat(binnum, "110"); break; case 7: strcat(binnum, "111"); break; } octnum = octnum/10; } printf("%s", binnum); } else printf("\nInvalid Octal Digit %d", rem); getch(); return 0; }

Here is the final snapshot of the above program's sample run:

As octal digit can not exceed the value greater than 7, therefore we have used **count** variable to check
whether any digit of the given octal number exceeds the value 7 or not. If it exceeds, then its value gets incremented
and then exits from the current loop. And before converting octal to binary, we have checked whether **count** holds its original value or not. If it holds, then
all the octal digit is correct, otherwise print any message like **Invalid Octal digit was entered by you**.

### Octal to Binary in C using Indirect Conversion

The question is, **Write a program in C that converts octal number to its equivalent binary number in such way that the given octal
number first converted into decimal number and then decimal number gets converted into binary number**. The answer to this question is:

#include<stdio.h> #include<conio.h> int main() { int octnum, decnum=0, binnum[40], rem, mul=1, i=0, count=0; printf("Enter any Octal Number: "); scanf("%d", &octnum); while(octnum!=0) { rem = octnum%10; if(rem>7) { count++; break; } decnum = decnum + (rem*mul); mul = mul*8; octnum = octnum/10; } if(count==0) { while(decnum!=0) { binnum[i] = decnum%2; i++; decnum = decnum/2; } printf("\nEquivalent Binary Value = "); for(i=(i-1); i>=0; i--) printf("%d", binnum[i]); } else printf("\nInvalid Octal Digit %d", rem); getch(); return 0; }

Here is the final snapshot of the above program's sample run:

### Octal to Binary in C using User-defined Function

To convert octal to binary using user-defined function named **OctToBin()**. Create this function in a way that
it takes one argument that will be given octal number by user.

Here we have declared all the common variables (present inside both the function main() and OctToBin()) as global variable
to make it known through both the function. The variable **i** and **count** gets declared as **static** variable,
so that they can remember their previous value. As we all knows that the static variable holds 0 as its initial value automatically.
Therefore here we have not initialized zero to both the static variable **i** and **count**.

#include<stdio.h> #include<conio.h> void OctToBin(int oct); static int i, count; int bin[40]; int main() { int octnum; printf("Enter any Octal Number: "); scanf("%d", &octnum); OctToBin(octnum); if(count==0) { printf("\nEquivalent Binary Value = "); for(i=(i-1); i>=0; i--) printf("%d", bin[i]); } else printf("\nYou've entered invalid Octal Digit"); getch(); return 0; } void OctToBin(int oct) { int dec=0, rem, mul=1; while(oct!=0) { rem = oct%10; if(rem>7) { count++; break; } dec = dec + (rem*mul); mul = mul*8; oct = oct/10; } if(count==0) while(dec!=0) { bin[i] = dec%2; i++; dec = dec/2; } }

Here is the final snapshot of the sample run:

#### Same Program in Other Languages

« Previous Program Next Program »

Like/Share Us on Facebook 😋