Author : Rahul
Write a program to convert decimal number to octal number
Program
using System;
namespace ProgrammingQuestion
{
class Program
{
static void Main(string[] args)
{
//Variable Declaration
string decimalNumberVal;
int decimalNumber, octalNumber = 0, tempNumber, digit, power = 0;
//User Input
Console.Write("Enter Decimal Number:");
decimalNumberVal = Console.ReadLine();
decimalNumber = Convert.ToInt32(decimalNumberVal);
//Decimal to Octal Logic
tempNumber = decimalNumber;
while (tempNumber > 0)
{
digit = tempNumber % 8;
octalNumber = octalNumber + digit * (Int32)Math.Pow(10, power);
power++;
tempNumber = tempNumber / 8;
}
//Print Result
Console.WriteLine($"Octal Number: {octalNumber}");
Console.Read();
}
}
}
Output
Enter Decimal Number:4586
Octal Number: 10752