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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.