Portfolio 2 Submission

Binary to Decimal Converter

Istvan Franko

Level 1 BSc

Lecturers:

Dr. Siobhan Devlin and Mr. Andrew Smith

Binary to Decimal Converter

Introduction

Source Code

Description of testing

Screen captures

References

Introduction

This portfolio will showcase what was learned over the last weeks. The program within a console command, will convert 3 binary numbers into decimals. With every binary input it checks whether the given value is correct, and in case of an incorrect input it warns you, and asks for a new input. After the conversion it shows the input binaries and the result in the decimal values. After writing out the result you have a chance to continue by inputting ’’Y’’, ’’y’’, ’’Yes’’ or ’’yes’’

Source Code

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace Portfolio2

{

class Program

{

static void Main(string[] args)

{

// declare variables int iNum1; int iNum2; int iNum3; int iDec; string sChoice;

sChoice = "Y"; // determine variable to first loop

//continuing control

while (sChoice == "Y" || sChoice == "y" || sChoice == "Yes" || sChoice ==

"yes")

{

// header

Console.WriteLine("Binary to decimal Converter"); Console.WriteLine("---------------------------");

// obtain first number

Console.WriteLine();

Console.Write("Enter digit 1: ");

iNum1 = Convert.ToInt32(Console.ReadLine());

// validate user input while (iNum1 > 1 || iNum1 < 0)

{

Console.Write("You must enter 0 or 1 , please re-enter: "); iNum1 = Convert.ToInt32(Console.ReadLine());

}

// obtain second number

Console.WriteLine();

Console.Write("Enter digit 2: ");

iNum2 = Convert.ToInt32(Console.ReadLine());

// validate user input while (iNum2 > 1 || iNum2 < 0)

{

Console.Write("You must enter 0 or 1 , please re-enter: "); iNum2 = Convert.ToInt32(Console.ReadLine());

}

// obtain third number

Console.WriteLine();

Console.Write("Enter digit 3: ");

iNum3 = Convert.ToInt32(Console.ReadLine());

// validate user input while (iNum3 > 1 || iNum3 < 0)

{

Console.Write("You must enter 0 or 1 , please re-enter: "); iNum3 = Convert.ToInt32(Console.ReadLine());

}

// calculate the result and show it iDec = iNum3 + iNum2 * 2 + iNum1 * 4;

Console.WriteLine();

Console.WriteLine(iNum1 + "" + iNum2 + "" + iNum3 + " converted to decimal is " + iDec);

// input to continue

Console.WriteLine();

Console.Write("Do you wish to continue? ");

sChoice = Console.ReadLine();

Console.Clear();

}

}

}

}

Description of testing

During the testing phase I have tried all possibilities of the three binary and their results.

The result table

Digit 1

Digit 2

Digit 3

Decimal

0

0

0

0

0

0

1

1

0

1

0

2

0

1

1

3

1

0

0

4

1

0

1

5

1

1

0

6

1

1

1

7

Screen captures

References

I didn't use any references to do my portfolio 2 only what I learned on the lectures and tutorials.

Big thanks to my teachers!

 

Notes