C Program to Convert Binary to Octal

C program to convert binary to Octal; Through this tutorial, we will learn how to convert binary number to Octal number in c program using for loop, while loop and function.

Programs to Convert Binary to Octal in C

Let’s use the following programs to convert binary to octal using while loop, for loop and function in c:

  • C Program to Convert Binary to Octal using While Loop
  • C Program to Convert Binary to Octal using For Loop
  • C Program to Convert Binary to Octal using Function

C Program to Convert Binary to Octal using While Loop

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

int main()
{
    int i, octal = 0, decimal = 0;
    long binary;

    printf("Enter the Binary Number = ");
    scanf("%ld", &binary);

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

    i = 1;
    while(decimal != 0)
    {
        octal = octal + (decimal % 8) * i;
        decimal = decimal / 8;
        i = i * 10;
    }
    printf("The octal Value = %d\n", octal);
}

The output of the above c program; as follows:

Enter the Binary Number = 121010
The octal Value = 112

C Program to Convert Binary to Octal using For Loop

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

int main()
{
    int i, remainder, octal = 0, decimal = 0;
    long binary;

    printf("Enter the Binary Number = ");
    scanf("%ld", &binary);

    for(i = 1; binary != 0; i = i * 2, binary = binary / 10)
    {
        remainder = binary % 10;
        decimal = decimal + remainder * i;
    }

    for(i = 1; decimal != 0; i = i * 10)
    {
        octal = octal + (decimal % 8) * i;
        decimal = decimal / 8;
    }
    printf("\nThe octal Value = %d\n", octal);
}

The output of the above c program; as follows:

Enter the Binary Number = 1101410
The octal Value = 172

C Program to Convert Binary to Octal using Function

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

int binaryTooctal(long binary)
{
    int octal = 0, i, decimal = 0;

    for(i = 0; binary != 0; i++)
    {
        decimal = decimal + (binary % 10) * pow(2, i);
        binary = binary/10;
    }
    for(i = 1; decimal != 0; i = i * 10)
    {
        octal = octal + (decimal % 8) * i;
        decimal = decimal / 8;
    }
    return octal;
}

int main()
{
    long binary;

    printf("Enter the Binary Number = ");
    scanf("%ld", &binary);

    printf("The octal Value = %d\n", binaryTooctal(binary));

    return 0;
}

The output of the above c program; as follows:

Enter the Binary Number = 11011011
The octal Value = 333

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *