Practical : 6
Subject : .Net
Output :
Subject : .Net
Aim : Write C# code to Convert binary to decimal,Convert decimal to hexadecimal,Convert decimal to binary,Convert decimal to octal.
Source Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dotnet008
{
class pra6
{
static void
Main(string[] args)
{
String d;
do
{
Console.WriteLine("\n---------Select Your Choice---------");
Console.WriteLine("1. binary to decimal \n2. decimal to hexadecimal
\n3. decimal to binary \n4. decimal to octal");
int n
= Convert.ToInt32(Console.ReadLine());
switch
(n)
{
case 1:
int num, r, dn = 0, b1 = 1;
Console.Write("Enter Binary number : ");
num = Convert.ToInt32(Console.ReadLine());
while (num > 0)
{
r = num % 10;
num = num / 10;
dn += r * b1;
b1 = b1 * 2;
}
Console.Write("Decimal Number : " + dn);
break;
case 2:
int den, quotient;
int i = 1, j, temp = 0;
char[] hexn = new char[100];
char temp1;
Console.Write("Enter Decimal
Number :");
den = int.Parse(Console.ReadLine());
quotient = den;
while (quotient != 0)
{
temp = quotient % 16;
if (temp < 10)
temp = temp +
48;
else
temp = temp +
55;
temp1 = Convert.ToChar(temp);
hexn[i++] = temp1;
quotient = quotient / 16;
}
Console.Write("HexaDecimal Number : ");
for (j = i - 1; j > 0; j--)
Console.Write(hexn[j]);
break;
case 3:
int[] a = new int[10];
Console.Write("Enter Decimal Number : ");
n = int.Parse(Console.ReadLine());
for (i = 0; n > 0; i++)
{
a[i] = n % 2;
n = n / 2;
}
Console.Write("Binary Number :
");
for (i = i - 1; i >= 0; i--)
{
Console.Write(a[i]);
}
break;
case 4:
int decVal, quot, ii = 1;
int[] octalVal = new int[80];
Console.Write("Enter Decimal Number : ");
decVal= int.Parse(Console.ReadLine());
quot = decVal;
while (quot != 0)
{
octalVal[ii++] = quot % 8;
quot = quot / 8;
}
Console.Write("Octal Number : ");
for (j = ii - 1; j > 0; j--)
Console.Write(octalVal[j]);
break;
}
Console.Write("\nDo You Want to Continue (Y/N)? : ");
d =
Console.ReadLine();
} while (d
== "y" || d == "Y");
Console.ReadKey();
}
}
}
0 comments:
Post a Comment