C# program to convert Decimal Number to Hexadecimal
Conversion of Decimal To Hexadecimal Number in C#
This C# Program Performs decimal to Hexadecimal Conversion. This C# Program converts the given decimal number into hexadecimal.
Store the decimal value in other variable (quotients) and store its modulo in temporary variable. Now compare the value of temporary variable. If it is less than 10 add 48 to it or else add 55 to it. and at last print the result.
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int deci, quotient, dn = 0, i,j;
int tmp;
int s;
Console.Write("Input any Decimal number: ");
deci = Convert.ToInt32(Console.ReadLine());
quotient = deci;
for (i = quotient; i > 0; i = i / 16)
{
tmp = i % 16;
if (tmp < 10)
tmp = tmp + 48;
else
tmp = tmp + 55;
dn = dn * 100 + tmp;
}
Console.Write("\nThe equivalent Hexadecimal Number : ");
for (j = dn; j > 0; j = j / 100)
{
s = j % 100;
Console.Write("{0}", (char)s);
}
Console.Write("\n");
Console.ReadKey();
}
}
}
Happy Coding :)
Related Post:
C# program to convert Binary Number to Decimal
C# program to convert Decimal Number to Binary
C# program to convert Decimal Number to Octal
Store the decimal value in other variable (quotients) and store its modulo in temporary variable. Now compare the value of temporary variable. If it is less than 10 add 48 to it or else add 55 to it. and at last print the result.
Here is source code of the C# Program to Perform Decimal to Hexadecimal Conversion. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int deci, quotient, dn = 0, i,j;
int tmp;
int s;
Console.Write("Input any Decimal number: ");
deci = Convert.ToInt32(Console.ReadLine());
quotient = deci;
for (i = quotient; i > 0; i = i / 16)
{
tmp = i % 16;
if (tmp < 10)
tmp = tmp + 48;
else
tmp = tmp + 55;
dn = dn * 100 + tmp;
}
Console.Write("\nThe equivalent Hexadecimal Number : ");
for (j = dn; j > 0; j = j / 100)
{
s = j % 100;
Console.Write("{0}", (char)s);
}
Console.Write("\n");
Console.ReadKey();
}
}
}
Output:
Input any Decimal number: 10
The equivalent Hexadecimal Number : A
Related Post:
C# program to convert Binary Number to Decimal
C# program to convert Decimal Number to Binary
C# program to convert Decimal Number to Octal
Comments
Post a Comment