- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Get Input from User
- C Print Integer
- C Add Two Numbers
- 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 Check Reverse equal Original
- C Add Subtract Multiply Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average, Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse a Number
- C Swap Two Numbers
- C Count Positive Negative Zero
- C Find Largest of Two Numbers
- C Find Largest 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 Area & Perimeter of Square
- C Area & Perimeter of Rectangle
- C Area & Circumference of Circle
- C Convert Fahrenheit to Celsius
- C Convert Celsius to Fahrenheit
- C Print ASCII Value
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find nCr and nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd's Triangle
- C Print Pascal's Triangle
- C Print Smiling face
- C 1D Array Programs
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- 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 2D Array Programs
- C Add Two Matrices
- C Subtract Two Matrices
- C Transpose a Matrix
- C Multiply Two Matrices
- C 3D Array 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 Delete Vowels from String
- C Delete Word from String
- C Find Frequency of Character
- C Count Word in String
- C Remove Spaces from String
- C Sort a String
- C Uppercase to Lowercase
- C Lowercase to Uppercase
- C Swap Two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read a File
- C Write Content to File
- C Read & Display File
- C Copy a File
- C Merge Two Files
- C List Files in Directory
- C Delete a File
- C Encrypt & Decrypt a File
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C More Programs
- C Check Palindrome String
- C Inches to Centimetres
- C Kilogram to Gram
- C Count Even Odd
- C Count Vowels Consonants
- C Find Profit Loss
- C Sum All Matrix Elements
- C First & Last Digit Sum
- C Largest Element in Matrix
- C Pass Array to Function
- C Address of Variable
- C Array Element at Even
- C Array Element at Odd
- C Print Even Array Elements
- C Print Odd Array Elements
- C Reverse File
- C Print Message with Time
- C Print Number in Words
- C Print Successive Character
- C Sum of their Square
- C Replace Vowel in String
- C Sort Words in Ascending
- C Sort Words in Descending
- C Sort String in Alphabetical
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

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.

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:

// Octal to Binary in C // ----codescracker.com---- #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}** =

- 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

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:

// Octal to Binary in C using strcat() Function // ----codescracker.com---- #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**.

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:

// Octal to Binary in C using Indirect Conversion // ----codescracker.com---- #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:

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**.

// Octal to Binary in C using Function // ----codescracker.com---- #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:

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.