Back to Home Page

/******************************************************************************
Problem: Write a program that converts a binary number to decimal notation.
Include a loop that lets the user repeat this computation for new
input values again and again until the user says he or she wants
to end the program.

Programmer: Guillermo Julca
Mercy College

View Output
*******************************************************************************/

using System;

namespace BinaryNumberToDecimal
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MyClass
{
static void Main(string[] args)
{
long binary;
int i;
double dec;
int dig;
char ans;
do
{
Console.Write("Enter a binary number :");
binary = Convert.ToInt64(Console.ReadLine());
dec = 0;
i= 0;
do
{
dig =Convert.ToInt16(binary % 10);
dec = dec + dig * Math.Pow(2.0,i);
binary = binary / 10;
i++;
}while ( binary != 0);
Console.WriteLine("The number in decimal base is = {0}",dec);
Console.Write("Continue (Y/N) ? : ");
ans = Convert.ToChar(Console.ReadLine());

}while (ans =='Y' || ans =='y');

}
}

}

//***************************************************************************************************************

View Output

Back to Home Page


                                                            GJ   Guillermo Julca