Showing posts with label binary to octal in c. Show all posts
Showing posts with label binary to octal in c. Show all posts

Tuesday, February 14, 2017

Binary To Octal Converter in C

Here is a simple program that I wrote using C as my programming language that will ask the user to give binary number and then our program will convert the given binary number into octal equivalent.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing

#include <stdio.h>
#include <math.h>

int Binary_Octal(long long binaryNumber)
{
    int octalNumber = 0, decimalNumber = 0, i = 0;

    while(binaryNumber != 0)
    {
        decimalNumber += (binaryNumber%10) * pow(2,i);
        ++i;
        binaryNumber/=10;
    }

    i = 1;

    while (decimalNumber != 0)
    {
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}

int main()
{
    long long binaryNumber;
    printf("\n==============================");
    printf("\nBinary To Octal Converter in C");
    printf("\n==============================");
    printf("\n\n");
    printf("Give a value in Binary : ");
    scanf("%lld", &binaryNumber);
    printf("\n\n");
    printf("The result is %lld in Binary = %d in Octal Value.", binaryNumber, Binary_Octal(binaryNumber));
    printf("\n\n");
    printf("THANK YOU FOR USING THIS PROGRAM");
    printf("\n\n");
}