Basic Programming C# Examples

C# Program to Convert Decimal to Octal Number without Array

The Program in C# Program to Convert Decimal to Octal Number without Array is given below:

using System;

namespace Decimal_to_Octal
{
    class Program
    {
        static void Main(string[] args)
        {
            int decimalNumber, remainder;
            string octalNumber = "";
 Console.WriteLine("Hello Codeauri Family,enter decimal number below to convert it into octal equivalent: ");
            Console.WriteLine("Well,enter a decimal number: ");
            decimalNumber = Convert.ToInt32(Console.ReadLine());

            while (decimalNumber > 0)
            {
                remainder = decimalNumber % 8;
                octalNumber = remainder + octalNumber;
                decimalNumber = decimalNumber / 8;
            }

            Console.WriteLine("Okay, the Octal representation of the decimal number is: " + octalNumber);
            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family,enter decimal number below to convert it into octal equivalent:
Well,enter a decimal number:
10
Okay, the Octal representation of the decimal number is: 12

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line using System; is a directive that tells the compiler to include the System namespace in the program, which is required to use the Convert class.
  2. The program has a class named Program that contains the Main method. The Main method is the entry point of the program.
  3. In the Main method, two integer variables decimalNumber and remainder are declared to store the decimal number and the remainder obtained after dividing the decimal number by 8, respectively.
  4. The octalNumber variable is declared as a string to store the octal representation of the decimal number.
  5. The Console.WriteLine method is used to prompt the user to enter a decimal number.
  6. The Convert.ToInt32 method is used to convert the input string to an integer and store it in the decimalNumber variable.
  7. The while loop is used to convert the decimal number into an octal number. The loop continues until the decimalNumber becomes 0.
  8. Inside the loop, the remainder after dividing the decimalNumber by 8 is calculated and stored in the remainder variable.
  9. The remainder is concatenated with the octalNumber string to form the octal representation of the decimal number.
  10. The decimalNumber is updated by dividing it by 8.
  11. The Console.WriteLine method is used to display the octal representation of the decimal number.
  12. The Console.ReadLine method is used to wait for the user to press the Enter key before closing the program.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

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

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X