A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C program to convert an binary number to octal
Q:

C program to convert an binary number to octal

belongs to collection: C programs - Basic C Programs

0

Writr C program takes a binary number as input and converts it to octal. Octal numbers have the base as 8. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to convert an binary number to octal - Source code

#include <stdio.h>
 
int main()
{
    long int binary_num, octal_num = 0, j = 1, rem;
 
    printf("Enter the binary number: ");
    scanf("%ld", &binary_num);
    while (binary_num != 0)
    {
        rem = binary_num % 10;
         binary_num = binary_num / 10;
        octal_num = octal_num + rem * j;
        j = j * 2;
 
    }
    printf("Equivalent octal value: %lo", octal_num);
    return 0;
}

Program Output

Enter the binary number: 10101101
Equivalent octal value: 255

Program Explanation

1. Take a binary number as input and store it in the variable binary_num.

2. Obtain the remainder and quotient of the input number by dividing it by 10.

3. Multiply the obtained remainder with variable j and increment the variable octal_num with this value.

4. Increment the variable j by multiplying it with 2 and override the variable binary_num with the quotient obtained.

5. Repeat the steps 2-4 until the variable binary_num becomes zero.

6. Print the variable octal_num as output.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to check whether two numbers are equal o... >>
<< C program to check if a number is divisible by 3...