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:
- 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 theConvert
class. - The program has a class named
Program
that contains theMain
method. TheMain
method is the entry point of the program. - In the
Main
method, two integer variablesdecimalNumber
andremainder
are declared to store the decimal number and the remainder obtained after dividing the decimal number by 8, respectively. - The
octalNumber
variable is declared as a string to store the octal representation of the decimal number. - The
Console.WriteLine
method is used to prompt the user to enter a decimal number. - The
Convert.ToInt32
method is used to convert the input string to an integer and store it in thedecimalNumber
variable. - The
while
loop is used to convert the decimal number into an octal number. The loop continues until thedecimalNumber
becomes 0. - Inside the loop, the remainder after dividing the
decimalNumber
by 8 is calculated and stored in theremainder
variable. - The
remainder
is concatenated with theoctalNumber
string to form the octal representation of the decimal number. - The
decimalNumber
is updated by dividing it by 8. - The
Console.WriteLine
method is used to display the octal representation of the decimal number. - 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.