In this tutorial you will learn about the C Program to Convert Octal to Binary and its application with practical example.
C Program to Convert Octal to Binary
In this tutorial, we will learn to create a C program that will Convert Octal to Binary in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
Program to Convert Octal to Binary:-
As we all know the c is a very powerful language. With the help of c programming language, we can make many programs. We cal perform many input-output operations using c programming. In today’s tutorial, we take the input in octal from the user and convert it into binary. With the help of c programming, we can perform many conversion operations.
With the help of this program, we can Convert Octal to binary.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Takeing the input number from from the user in octal for the program. 3. Passing that input to the string function. 4. Pass that number to a for loop for convertion. 4. Print the Result. 5. End the program. |
Program to Convert Octal to Binary:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <math.h> #include <stdio.h> long long convert(int oct); int main() { //decalring the variable for the program int oct; //taking input from the user in octal number printf("Enter an octal number: "); //scanning the octal number. scanf("%d", &oct); //result printing after convertion printf("%d in octal = %lld in binary", oct, convert(oct)); return 0; } //function for converting the ocatl to decimal and then to binary. long long convert(int oct) { int dec = 0, i = 0; long long bin = 0; // converting octal to decimal while (oct != 0) { dec += (oct % 10) * pow(8, i); ++i; oct /= 10; } i = 1; // converting decimal to binary while (dec != 0) { bin += (dec % 2) * i; dec /= 2; i *= 10; } return bin; } |
Output:-
In the above program, we have first initialized the required variable.
- oct = in will hold the integer value.
Taking the input.