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

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

Number conversion is a common programming exercise that helps improve problem-solving skills. In this tutorial, we will discuss a program to convert octal numbers to decimal and vice-versa using C# programming language.

Syntax

The syntax for converting an octal number to decimal in C# is as follows:

int octal = 763;
int decimal = 0;
int baseNumber = 1;
while (octal > 0) {
    int digit = octal % 10;
    octal /= 10;
    decimal += digit * baseNumber;
    baseNumber *= 8;
}

The syntax for converting a decimal number to octal in C# is as follows:

int decimalNumber = 345;
string octalNumber = "";
while (decimalNumber > 0) {
    int rem = decimalNumber % 8;
    octalNumber = rem + octalNumber;
    decimalNumber /= 8;
}

Example

Octal to Decimal

using System;

namespace octal_decimal_conversion
{
    class Program
    {
        static void Main(string[] args)
        {
            int octalNumber = 763;
            int decimalNumber = 0;
            int baseNumber = 1;
            while (octalNumber > 0)
            {
                int digit = octalNumber % 10;
                octalNumber /= 10;
                decimalNumber += digit * baseNumber;
                baseNumber *= 8;
            }
            Console.WriteLine("Octal 763 = " + decimalNumber);
        }
    }
}

Output:

Octal 763 = 499

Decimal to Octal

using System;

namespace octal_decimal_conversion
{
    class Program
    {
        static void Main(string[] args)
        {
            int decimalNumber = 345;
            string octalNumber = "";
            while (decimalNumber > 0)
            {
                int rem = decimalNumber % 8;
                octalNumber = rem + octalNumber;
                decimalNumber /= 8;
            }
            Console.WriteLine("Decimal 345 = Octal " + octalNumber);
        }
    }
}

Output:

Decimal 345 = Octal 531

Explanation

To convert an octal number to decimal, we need to multiply each digit of the octal number by successive powers of 8, starting from 8^0 and going up to 8^n, where n is the number of digits in the octal number minus 1. We then add all of these products together to get the decimal equivalent.

To convert a decimal number to octal, we need to divide the decimal number by 8 repeatedly and note down the remainder each time, starting from the right of the number. The resulting remainders, read from right to left, give the octal equivalent of the decimal number.

Use

Converting numbers from one base to another is an important skill in computer programming. These kinds of programs can be used to convert numbers between different number systems, such as binary, decimal, and hex, and can form the basis for more complex arithmetic and mathematical calculations.

Summary

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

Published on: