Decimal to Binary
Given that one of the interesting problem is transferring Decimal number to a binary number, in the appendix you will find a complete solution SharpDevelop IDE written in the C# programming language. Below is just a part of the code that is most important to solve this problem.
Note :
This program solves the problem only in the set of integers.
Program code:
/*
* Created by SharpDevelop.
* User: Perić Željko
* Date: 09.02.2012
* Time: 17:02
*
* Simple console program for conversion of
* Decimal to Binary numbers
*
*/
using System;
namespace Decimal_to_Binary
{
class Program
{
public static void Main(string[] args)
{
//
// declaration of variables
//
string Decimal = "";
string Binary = "";
string Letter = "";
int Index = 0;
int Lenght = 0;
double Cipher = 0;
double Integral = 0;
double DecimalNumber = 0;
double DivisionResult = 0;
double MultiplicationResult = 0;
bool DecimalRelevance = false;
//
// not very desirable but necessary
// begining of program label
//
begining :
//
// Hello message to user
//
Console.Title = "Program Decimal to binary ";
Console.SetWindowSize(80,40);
Console.ForegroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("****************************************************************************");
Console.WriteLine(" Decimal to binary");
Console.WriteLine("****************************************************************************");
//
// Get number from Console
//
Console.WriteLine();
Console.Write("Enter decimal number (exmpl: 49 = 110001) : ");
Decimal = "";
Decimal = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("****************************************************************************");
Console.WriteLine();
//
// Check if it is Decimal number
//
DecimalRelevance = false;
Lenght = Decimal.Length;
Index = 0;
Letter = "";
while(Lenght > Index)
{
Letter = Decimal.Substring(Index,1);
DecimalRelevance = double.TryParse(Letter,out Cipher);
if (DecimalRelevance == true)
{
Index = Index + 1;
}
else
{
Index = Lenght;
}
}
if (DecimalRelevance != true)
{
Console.WriteLine("Wrong entry.....");
Console.WriteLine();
//
// not very desirable but necessary
// end of program label
//
goto end;
}
else
{
DecimalNumber = double.Parse(Decimal);
Decimal = DecimalNumber.ToString();
}
//
// Resolving conversion from Decimal to Binary
//
Binary = "";
Integral = 0;
DivisionResult = 0;
MultiplicationResult = 0;
Cipher = 0;
while (DecimalNumber != 0 )
{
DivisionResult = DecimalNumber/ 2;
Integral = Math.Truncate(DivisionResult);
MultiplicationResult = 2 * Integral ;
Cipher = DecimalNumber - MultiplicationResult;
DecimalNumber = Integral;
Binary = Binary.Insert(0,Cipher.ToString());
}
//
// if entered number where zero
//
if (Binary == "")
{
Binary = "0";
}
//
// Write result of conversion
//
Console.WriteLine("Decimal number : " + Decimal);
Console.WriteLine();
Console.WriteLine("Binary number : " + Binary);
Console.WriteLine();
//
// not very desirable but necessary
// end of program label
//
end:
Console.WriteLine("****************************************************************************");
Console.WriteLine();
Console.Write("Do you want to run program again y / n : ");
Letter = Console.ReadLine();
if ( Letter == "Y" || Letter == "y")
{
goto begining;
}
}
}
}
All the best,
Author