using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program1
{
static void Main(string[] args)
{
double sum = 0;
Console.WriteLine("Enter the Binary Number : ");
string strBinary = Console.ReadLine();
int Len = strBinary.Length;
int slen = 0;
int DecimalPosition = 0;
int counter = 1;
bool flag = false;
if (strBinary.Contains("."))
{
flag = true;
DecimalPosition = strBinary.IndexOf(".");
slen = strBinary.Substring(DecimalPosition+1,Len-DecimalPosition-1).Length;
foreach (var u in strBinary.Substring(DecimalPosition + 1, Len - DecimalPosition - 1))
{
if (u.ToString() == "1")
{
sum = sum + Math.Pow(2, ((counter) * -1));
}
counter++;
}
}
if (flag == true)
{
slen = strBinary.Substring(0, DecimalPosition).Length;
foreach (var s in strBinary.Substring(0, DecimalPosition))
{
if (s.ToString() == "1")
{
sum = sum + Math.Pow(2, slen - 1);
}
slen--;
}
}
if (flag == false)
{
foreach (var s in strBinary)
{
if (s.ToString() == "1")
{
sum = sum + Math.Pow(2, Len - 1);
}
Len--;
}
}
Console.WriteLine("Decimal Number :");
Console.Write(sum);
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program1
{
static void Main(string[] args)
{
double sum = 0;
Console.WriteLine("Enter the Binary Number : ");
string strBinary = Console.ReadLine();
int Len = strBinary.Length;
int slen = 0;
int DecimalPosition = 0;
int counter = 1;
bool flag = false;
if (strBinary.Contains("."))
{
flag = true;
DecimalPosition = strBinary.IndexOf(".");
slen = strBinary.Substring(DecimalPosition+1,Len-DecimalPosition-1).Length;
foreach (var u in strBinary.Substring(DecimalPosition + 1, Len - DecimalPosition - 1))
{
if (u.ToString() == "1")
{
sum = sum + Math.Pow(2, ((counter) * -1));
}
counter++;
}
}
if (flag == true)
{
slen = strBinary.Substring(0, DecimalPosition).Length;
foreach (var s in strBinary.Substring(0, DecimalPosition))
{
if (s.ToString() == "1")
{
sum = sum + Math.Pow(2, slen - 1);
}
slen--;
}
}
if (flag == false)
{
foreach (var s in strBinary)
{
if (s.ToString() == "1")
{
sum = sum + Math.Pow(2, Len - 1);
}
Len--;
}
}
Console.WriteLine("Decimal Number :");
Console.Write(sum);
Console.Read();
}
}
}
No comments:
Post a Comment