## Converting Decimal to Hexadecimal in C#

In the decimal system, each digit position represents a power of 10. For example, the number 1234 can be broken down as follows:

`1 * 10^3 + 2 * 10^2 + 3 * 10^1 + 4 * 10^0`

Hexadecimal follows a similar logic, but each digit position represents a power of 16. The hexadecimal digits are 0-9 followed by A-F, where A represents 10, B represents 11, and so on.

## Conversion Algorithm

To convert a decimal number to hexadecimal, we repeatedly divide the decimal number by 16 and collect the remainders until the quotient becomes zero. Then, we read the remainders in reverse order to get the hexadecimal equivalent.

```
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a decimal number: ");
int decimalNumber = int.Parse(Console.ReadLine());
string hexadecimalNumber = "";
while (decimalNumber > 0)
{
int remainder = decimalNumber % 16;
if (remainder < 10)
hexadecimalNumber = remainder + hexadecimalNumber;
else
hexadecimalNumber = (char)(remainder - 10 + 'A') + hexadecimalNumber;
decimalNumber /= 16;
}
Console.WriteLine("Hexadecimal equivalent: " + hexadecimalNumber);
}
}
```

### Output:

```
Enter a decimal number: 255
Hexadecimal equivalent: FF
```

## Comments (0)