This programme will convert a denary number into a binary number.
00:06
We'll introduce you to modulo and integer division.
00:11
As always, we start with the boilerplate code in the main programme.
00:18
We'll output a message asking the user to enter a denary number.
00:23
We'll store this in an integer variable called number and use convert to cast to an integer the line input from the keyboard.
00:32
Next, we're going to output the result.
00:35
So console, writeline, bracket, quote, followed by a message to explain what the output will be and the output from the function we'll write in a minute called then two bin, which is short for denary to binary.
00:51
This will require the number, so open bracket, the number we input.
00:55
Close bracket, close bracket.
00:59
Now we need to define the function dent that does the conversion static string, then to bin, which will have the parameter passed in number, which is an integer.
01:12
Worth noting here that we're not going to return an actual number, we're going to return a string which will have the characters for zeros and ones representing our binary output.
01:24
So the algorithm works like this.
01:26
We keep dividing the number by two and rounding down.
01:30
If the number was exactly divisible by 2, we want to join a 0 to the binary number and if not, we'll join a 1.
01:38
We do this while the number is above 0.
01:42
So a string binary that will store our answer is assigned to be an empty string.
01:48
To begin with, strings are qualified with quotes, so quote, quote, while number is greater than zero.
01:58
The remainder is the number divided by two, but the remainder, not decimal places.
02:05
Getting the remainder from a division is achieved using the modulo operator and in C, that's the percentage symbol.
02:13
Imagine you've got six suites and divide by two people.
02:17
Six divided by two is three sweets each, with no sweets left over, no remainder.
02:22
however, seven sweets divided by two is three, each with one left over.
02:29
The remainder, is one.
02:32
So six modulo two is zero, but seven modulo two is one.
02:40
Therefore, what we get as a result of dividing by 2 is either 0 or 1 as a remainder or modulus.
02:48
In our programme, remainder is 0 or 1.
02:51
At this point we want to join this to our binary string.
02:56
But because remainder is a number and binary is a string, we have to cast remainder to become a string to be able to join or concatenate it.
03:06
Remember, the binary code inside a computer for a number is different to the binary code for the characters 0 or 1.
03:14
Numbers are used for maths and strings are used for output to the screen.
03:20
Binary becomes the string of the remainder.
03:22
plus whatever the binary number is.
03:24
Now, interestingly, we can use the plus operator to both add numbers and concatenate strings together.
03:34
Number equals itself divided by two.
03:38
Now, notice a clever trick here.
03:40
Number was declared as an integer parameter when we defined the thentobin function.
03:46
This means if there are any decimal places after a division, they cannot be stored and are lost.
03:53
You might expect a syntax error here.
03:55
usually we have to cast one data type into another using convert to.
04:02
However, with simple division, this is not actually necessary.
04:07
Truncating a double number to an integer when dividing is also known as integer division.
04:15
Once the condition in the while loop is true, it will end, so we can return the string of characters we called binary that's representing our binary number.
04:25
This is one of those algorithms that illustrates why people find programming so difficult.
04:30
When we explained how the algorithm was going to work, we said if the remainder is zero, yet in our programme there is no if command.
04:39
That's because remainder can only be 0 or 1 after the modulo division, and we want to join it to the binary number either way.
04:47
So we didn't need to use an if we could.
04:50
But it's unnecessary because the joining of either 0 or 1 will happen.
04:55
Whatever the case, let's give the programme a run.