C Program to Convert Binary Number to Octal and Vice-Versa
ADVERTISEMENTS
C program to convert binary number to octal and vice-versa.
In this article you will learn how to convert the binary number into octal number and second convert octal number to binary number.
Example-1
Enter Binary Number: 11001010101
11001010101 (Binary Number) = 3125 (Octal Number)
Example-2
Enter Octal Number: 150
150 (Octal Number) = 1101000 (Binary Number)
C program to convert binary number to octal Number
// C program to convert binary number to octal Number
#include <stdio.h>
#include <math.h>
// It's main function of the program
int main() {
long long binary_num, temp_binary_num;
int decimal_num, octal_num, i = 0;
printf("Enter Binary Number: ");
scanf("%lld", &temp_binary_num);
binary_num = temp_binary_num;
// Step-1 make conversion binary to decimal
while (temp_binary_num != 0) {
decimal_num += (temp_binary_num % 10) * pow(2, i);
++i;
temp_binary_num /= 10;
}
i = 1;
// Step-2 then make conversion decimal to octal
while (decimal_num != 0) {
octal_num += (decimal_num % 8) * i;
decimal_num /= 8;
i *= 10;
}
// Final output of the program
printf("\n%lld (Binary Number) = %d (Octal Number)\n", binary_num, octal_num);
return 0;
}
Output
Enter Binary Number: 11001010101
11001010101 (Binary Number) = 3125 (Octal Number)
C program to convert octal Number to binary number
// C program to convert octal Number to binary number
#include <stdio.h>
#include <math.h>
// It's main function of the program
int main() {
int decimal_num, octal_num, temp_octal_num, i = 0;
long long binary_num;
printf("Enter Octal Number: ");
scanf("%d", &temp_octal_num);
octal_num = temp_octal_num;
// Step-1 make conversion octal to decimal
while (temp_octal_num != 0) {
decimal_num += (temp_octal_num % 10) * pow(8, i);
++i;
temp_octal_num /= 10;
}
i = 1;
// Step-2 then make conversion decimal to binary
while (decimal_num != 0) {
binary_num += (decimal_num % 2) * i;
decimal_num /= 2;
i *= 10;
}
// Final output of the program
printf("\n%d (Octal Number) = %lld (Binary Number)\n", octal_num, binary_num);
return 0;
}
Output
Enter Octal Number: 150
150 (Octal Number) = 1101000 (Binary Number)