Convert Decimal to Binary Using Recursion Using Python Codes
Technology: Problem is to write a python program to convert decimal to binary using recursion. This Post we will learn how to write a program that will allow us to convert a decimal number into a binary number using a recursion method first of all talking about what are decimal numbers let’s understand that decimal numbers are all the numbers that we have from 0 to 9 and all the other numbers that we have are the combination of these 10 numbers.
Convert Decimal to Binary
Convert Decimal to Binary
Not talking about the binary number binary numbers are two numbers that is 0 and 1 where 0 stands for off and 1 stands for on now lastly talking about the recursion recursion is a process in which a function call itself inside a function now let’s talk about all of them one by one first of all talking about decimal numbers decimal numbers are 0 1 2 3 4 5 6 7 8 and 9. so these are our decimal numbers and all the other numbers are created by these nine numbers only these 10 numbers only right next talking about the binary numbers so binary numbers are 0 and 1 that means.
There are two numbers in the binary numbers here in the decimal numbers we have 10 numbers in total okay lastly talking about the recursion so recursion is a process where a function calls itself inside a function now what do we need to do we need to create a program which will convert a decimal number into a binary number using a process of recursion first of all talking about converting a decimal number into a binary number so when we do it on a paper that means when we do it on the math how do we do it suppose we have any number let’s say it’s 15 so how do.
Convert it into a binary number we need to divide it by 2 that means so binary term over here means 2. so any number if you take any decimal number if you take if you want to convert it into a binary number then you have to divide it by 2 when you divide it by 2 you get the output 7 over here that means 2 times 7 is 14 and we get the 1 remainder right similarly again we will divide it so 2 times 3 is 6 and we get 1 as the remainder then 2 times 1 is 2 and we’ll get the remainder as 1. now what will be the binary number over here the binary.
Number will be this number over here so these numbers that mean these string over here will be our binary number so the binary number for 15 will not be 1 1 1 1 in this way but it will be 1 then this one then this one and then this one that means when we write the binary string we go all the way opposite so that means our binary number for 15 would be 1 1 1 and 1. similarly if i do it with any other number let’s say if i do it with 10 so if i’ll write 2 so it will be 5 and the remainder would be 0 2 2 times is 4 remainder would be 1 2 1 time is 2 and the remainder would be 0 right now what will be the output that means what will be the binary number.
Of 10 it won’t be 0 1 0 1 it will it will be 1 0 1 0 this is how a binary string is written down this is how we create a binary number now this is in the math what if we want to do it in the python so so for doing it in a python we have a very simple method to convert our decimal number to binary all we need to do is that we need to create a function d e f and i want to convert it from decimal to binary so d to b i’ll be writing it so d to b is the name of my function inside i’ll be passing a variable called as n what do i need to do over here is now that if.
My n number is greater than 1 in that condition whatever that number is suppose that number is let’s say 12 okay so what do i need to do i need to do the flow division of it to get the to get the quotient over here so number flow division 2 and we have to put it inside d2 b that means he will call this function over and over again till the value is less than one okay after this here what we need to do we need to print and model is two that means remainder of this sort well.
When divided by two whatever remainder we’ll get so we’ll get the remainder zero over here that 12 6 time is 12 and we’ll get the remainder 0 over here that means the remainder 0 will come over here then we will write end is equals to open close quotations to again i trade this that means this is this will work like a function over here that means it will call itself again that means d to b is a function which is calling itself again inside the function that is our process of recursion that it is calling itself inside the function now next time.
What d to b will be it will be our 6 so 6 when divided by 2 and 6 modulus 2 that would be our 2 3 times is 6 remainder is 0 again so next value would be 0 again so we have now 0 and 0. now this time we have 3 so 3 floor division 2 would be 1 and the remainder over here will also be 1 that 3 when divided by 2 gives us 1 that 2 1 is 2 and the remainder is 1 so next value would be 1 and lastly the last value the last remaining value will again become 1 so the value would not be 0 0 1 1 it will be 1 1 0 and 0 this is how it will work now let’s put this similar logic inside our.
Program so i’ll be writing def convert binary because we want to convert it to binary so i’ll be creating a function that is called as convert binary and i will be passing a parameter called as n over here inside this i’ll be giving a condition that if n is greater than one only if n is greater than one in that condition what do we need to do we need to call this function convert binary and inside we have to pass n flow division two why every time we are taking the value two because we have to convert it into a binary and the word binary means 2 over here and then.
We’ll be printing n modulus 2 that means what is the remainder of n when it is divided by 2 comma end is equals to open close quotations lastly all we need to do is take the input from the user for the n so n is equals to int input enter a number here next we need to print the converted value in binary is i’ll put a call on over here and i’ll call the function in the next line that is convert binary and inside i’ll pass the value let’s say i want to check it for number 15 we know for number.
15 the value was 1 1 1 1 let’s say if our program says the same as well so i don’t have to write 15 actually over here because i’m taking the user inputs i’ll be writing n only so if i run it if i write 15 over here it gives me the converted value in binary is 1 1 1 1 that means our program is working perfectly fine and if i check it for 12 for 12 we found out that the value was double one double zero let’s see if it is double one double zero or not so let’s run and if i write 12 it says double one double zero that means our method over here is working perfectly fine so all we needed to do was create a function called as convert binary and pass a parameter n and we need to give a condition that only if n is greater than one in that condition.
Will do we will convert the binary that means we’ll call the function again and whatever is the quotient of n when divided by 2 that means in flow division 2 will give us the quotient whatever the quotient will be there that will be our value of next n and we will print what we will print the modulus of n when divided by 2 that means n when divided by 2 whatever remainder gives.