2017-09-30 15:57:21 +03:00
|
|
|
// Binary number to octal number conversion
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdio.h>
|
2017-09-30 15:57:21 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
// Function that returns the last three digits
|
2017-09-30 15:57:21 +03:00
|
|
|
int three_digits(int n)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int r, d = 0, p = 1;
|
2017-09-30 15:57:21 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
for (int i = 0; i < 3; i++)
|
2017-09-30 15:57:21 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
r = n % 10;
|
2018-03-28 19:11:54 +03:00
|
|
|
d += r * p;
|
|
|
|
p *= 10;
|
|
|
|
n /= 10;
|
2017-09-30 15:57:21 +03:00
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int binary_num, d = 0, base = 1, remainder, td, res = 0, ord = 1;
|
2017-09-30 15:57:21 +03:00
|
|
|
|
|
|
|
printf("Enter the binary no: ");
|
|
|
|
scanf("%d", &binary_num);
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
while (binary_num > 0)
|
2017-09-30 15:57:21 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
if (binary_num >
|
|
|
|
111) // Checking if binary number is greater than three digits
|
2017-09-30 15:57:21 +03:00
|
|
|
td = three_digits(binary_num);
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
else
|
|
|
|
td = binary_num;
|
2017-09-30 15:57:21 +03:00
|
|
|
|
2018-03-28 19:11:54 +03:00
|
|
|
binary_num /= 1000;
|
2017-09-30 15:57:21 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
d = 0, base = 1;
|
2017-09-30 15:57:21 +03:00
|
|
|
|
|
|
|
// Converting the last three digits to decimal
|
2020-05-29 23:23:24 +03:00
|
|
|
while (td > 0)
|
2017-09-30 15:57:21 +03:00
|
|
|
{
|
|
|
|
remainder = td % 10;
|
2018-03-28 19:11:54 +03:00
|
|
|
td /= 10;
|
|
|
|
d += (base * remainder);
|
|
|
|
base *= 2;
|
2017-09-30 15:57:21 +03:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:11:54 +03:00
|
|
|
res += d * ord; // Calculating the octal value
|
|
|
|
ord *= 10;
|
2017-09-30 15:57:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nOctal equivalent is: %d", res);
|
|
|
|
return 0;
|
|
|
|
}
|