C# Program to Convert Decimal to Octal
Posted by Superadmin on August 13 2022 10:15:29

C# Program to Convert Decimal to Octal

 

 

This is a C# Program to convert decimal to octal.

Problem Description

This C# Program Converts Decimal to Octal.

Problem Solution

Here the decimal number is first obtained from the user and Is converted to octal.

Program/Source Code

Here is source code of the C# Program to Convert Decimal to Octal. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Convert Decimal to Octal
 */
using System;
class program
{
    public static void Main()
    {
        int decimalNumber, quotient, i = 1, j;
        int[] octalNumber = new int[100];
        Console.WriteLine("Enter a Decimal Number :");
        decimalNumber = int.Parse(Console.ReadLine());
        quotient = decimalNumber;
        while (quotient != 0)
        {
            octalNumber[i++] = quotient % 8;
            quotient = quotient / 8;
        }
        Console.Write("Equivalent Octal Number is ");
        for (j = i - 1; j > 0; j--)
            Console.Write(octalNumber[j]);
        Console.Read();
    }
}
Program Explanation

In this C# program, we are reading the decimal number using ‘decimalnumber’ variable. Octal is a numbering system that uses eight digits, 0 to 7, arranged in a series of columns to represent all numerical quantities. Each column or place value has a weighted value of 1, 8, 64, 512, and so on ranging from right to left. Decimal is a term that describes the base-10. Using while loop checks the condition that the value of ‘quotient’ variable is not equal to 0. If the condition is true then execute the condition.

 

The octalnumber[] array variable is used to compute the modulus of the value of ‘quotient’ variable by 8. Compute the division of the value of ‘quotient’ variable by 8. For loop is used to print the octal number of the decimal value.

Runtime Test Cases
 
Enter a Decimal Number :
50
Equivalent Octal Number is 62