Practical : 7
Subject : .NET
Aim :
Write C# code to do the following
- Convert binary to decimal
- Convert decimal to hexadecimal
- Convert decimal to binary
Code:
using System;
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter your Choice : ");
Console.WriteLine("1. Binary to Decimal");
Console.WriteLine("2. Decimal to Hexadecimal");
Console.WriteLine("3. Decimal to Binary");
Console.WriteLine("4. Decimal to octal");
Console.WriteLine("5. All");
Console.WriteLine("6. Exit");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
B2D();
break;
case 2:
D2H();
break;
case 3:
D2B();
break;
case 4:
D2O();
break;
case 5:
B2D();
D2H();
D2B();
D2O();
break;
case 6:
break;
default:
Console.WriteLine("Enter valid choice");
break;
}
if (choice == 6)
break;
}
Console.ReadKey();
}
static void B2D()
{
Console.WriteLine("Enter a binary number:");
String binary = Console.ReadLine();
char[] b = binary.ToCharArray();
int i = 0;
int[] r = new int[20];
r[0] = 1;
int t = 1;
for (i = 1; i < 20; i++)
{
t = t * 2;
r[i] = t;
}
int d = 0,j=0;
for(i=b.Length-1;i>=0;i--)
{
if(b[i]=='1')
{
d = d + r[j];
}
j++;
}
Console.WriteLine("Decimal number:\t" + d);
}
static void D2H()
{
Console.WriteLine("Enter a Decimal Number:");
int d = int.Parse(Console.ReadLine());
char []t = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
int i = 0;
char[] h = new char[10];
while(d>0)
{
int temp = d % 16;
h[i] = t[temp];
d = d / 16;
i++;
}
Console.Write("Hexadecimal Number:\t");
i--;
for(;i>=0;i--)
{
Console.Write(h[i]);
}
Console.Write("\n");
}
static void D2B()
{
Console.WriteLine("Enter a decimal number:");
int d = int.Parse(Console.ReadLine());
int[] b = new int[15];
int i = 0;
while (d > 0)
{
int temp = d % 2;
b[i] = temp;
d = d / 2;
i++;
}
i--;
Console.Write("Binary Number:\t");
for(;i>=0;i--)
{
Console.Write(b[i]);
}
Console.Write("\n");
}
static void D2O()
{
Console.WriteLine("Enter a Decimal Number:");
int d = int.Parse(Console.ReadLine());
char[] t = { '0', '1', '2', '3', '4', '5', '6', '7'};
int i = 0;
char[] h = new char[10];
while (d > 0)
{
int temp = d % 8;
h[i] = t[temp];
d = d / 8;
i++;
}
Console.Write("Octal Number:\t");
i--;
for (; i >= 0; i--)
{
Console.Write(h[i]);
}
Console.Write("\n");
}
}
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter your Choice : ");
Console.WriteLine("1. Binary to Decimal");
Console.WriteLine("2. Decimal to Hexadecimal");
Console.WriteLine("3. Decimal to Binary");
Console.WriteLine("4. Decimal to octal");
Console.WriteLine("5. All");
Console.WriteLine("6. Exit");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
B2D();
break;
case 2:
D2H();
break;
case 3:
D2B();
break;
case 4:
D2O();
break;
case 5:
B2D();
D2H();
D2B();
D2O();
break;
case 6:
break;
default:
Console.WriteLine("Enter valid choice");
break;
}
if (choice == 6)
break;
}
Console.ReadKey();
}
static void B2D()
{
Console.WriteLine("Enter a binary number:");
String binary = Console.ReadLine();
char[] b = binary.ToCharArray();
int i = 0;
int[] r = new int[20];
r[0] = 1;
int t = 1;
for (i = 1; i < 20; i++)
{
t = t * 2;
r[i] = t;
}
int d = 0,j=0;
for(i=b.Length-1;i>=0;i--)
{
if(b[i]=='1')
{
d = d + r[j];
}
j++;
}
Console.WriteLine("Decimal number:\t" + d);
}
static void D2H()
{
Console.WriteLine("Enter a Decimal Number:");
int d = int.Parse(Console.ReadLine());
char []t = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
int i = 0;
char[] h = new char[10];
while(d>0)
{
int temp = d % 16;
h[i] = t[temp];
d = d / 16;
i++;
}
Console.Write("Hexadecimal Number:\t");
i--;
for(;i>=0;i--)
{
Console.Write(h[i]);
}
Console.Write("\n");
}
static void D2B()
{
Console.WriteLine("Enter a decimal number:");
int d = int.Parse(Console.ReadLine());
int[] b = new int[15];
int i = 0;
while (d > 0)
{
int temp = d % 2;
b[i] = temp;
d = d / 2;
i++;
}
i--;
Console.Write("Binary Number:\t");
for(;i>=0;i--)
{
Console.Write(b[i]);
}
Console.Write("\n");
}
static void D2O()
{
Console.WriteLine("Enter a Decimal Number:");
int d = int.Parse(Console.ReadLine());
char[] t = { '0', '1', '2', '3', '4', '5', '6', '7'};
int i = 0;
char[] h = new char[10];
while (d > 0)
{
int temp = d % 8;
h[i] = t[temp];
d = d / 8;
i++;
}
Console.Write("Octal Number:\t");
i--;
for (; i >= 0; i--)
{
Console.Write(h[i]);
}
Console.Write("\n");
}
}
Output:
Sign up here with your email
ConversionConversion EmoticonEmoticon