c-sharp
  1. c-sharp-program-to-convert-binary-number-to-octal-and-vice-versa

Program to Convert Binary Number to Octal and vice-versa - (C# Basic Programs)

Binary and octal number systems are commonly used in computer science, and it is often necessary to convert between the two. In this tutorial, we will discuss a program to convert binary numbers to octal and vice-versa using C# programming language.

Syntax

The syntax for converting binary numbers to octal and vice-versa in C# is as follows:

Conversion from binary to octal

string binaryNumber = "101010";
int decimalNumber = Convert.ToInt32(binaryNumber, 2); 
string octalNumber = Convert.ToString(decimalNumber, 8);

Conversion from octal to binary

string octalNumber = "52";
int decimalNumber = Convert.ToInt32(octalNumber, 8);
string binaryNumber = Convert.ToString(decimalNumber, 2);

Example

Conversion from binary to octal

using System;

namespace BinaryToOctal
{
    class Program
    {
        static void Main(string[] args)
        {
            string binaryNumber = "101010";
            int decimalNumber = Convert.ToInt32(binaryNumber, 2);
            string octalNumber = Convert.ToString(decimalNumber, 8);
            Console.WriteLine("Binary number: {0}", binaryNumber);
            Console.WriteLine("Octal number: {0}", octalNumber);
        }
    }
}

Output:

Binary number: 101010
Octal number: 52

Conversion from octal to binary

using System;

namespace OctalToBinary
{
    class Program
    {
        static void Main(string[] args)
        {
            string octalNumber = "52";
            int decimalNumber = Convert.ToInt32(octalNumber, 8);
            string binaryNumber = Convert.ToString(decimalNumber, 2);
            Console.WriteLine("Octal number: {0}", octalNumber);
            Console.WriteLine("Binary number: {0}", binaryNumber);
        }
    }
}

Output:

Octal number: 52
Binary number: 101010

Explanation

To convert a binary number to octal, we first need to convert it to decimal using the Convert.ToInt32() method, which takes the binary number and the base as input arguments. We then convert the decimal number to octal using the Convert.ToString() method, which takes the decimal number and the base as input arguments.

To convert an octal number to binary, we follow the same process in reverse. We first convert the octal number to decimal using the Convert.ToInt32() method and then convert the decimal number to binary using the Convert.ToString() method.

Use

Converting binary numbers to octal and vice versa is a common task in computer science and is often used in programming and other applications. By understanding how to convert between these number systems, programmers can write more efficient code and solve complex problems.

Summary

In this tutorial, we have discussed a program to convert binary numbers to octal and vice-versa using C# programming language. We have seen the syntax, examples, explanation, and use of converting between binary and octal numbers in C#. By practicing these exercises, programmers can improve their problem-solving skills and become better equipped to tackle complex coding challenges.

Published on: