(DecBin2) Write a program that will accept a number in decimal and display the number in binary. In Programming Assignment 2, you wrote DecBinl which converted and displayed a decimal number in binary for BUT your program was limited to decimal numbers between 0 and 15 due to the fixed number of bits being sought ( four). In this programming assignment we seek to remedy this limitation! (DecBin1) Consider the conversion of a non-negative decimal (base 10) integer to base 2 (binary). We will assume the non- negative base 10 integer has value between 0 and 15 inclusive. As a guiding example, let's convert (13), to binary (base 2). Using long division, we successively divide by the base to which we are converting, 2 in this case. 6 3 1 0 2)13 26 -12 6 0 101 1 The resulting binary number is (1101), (writing the remainders as read from right to left). Now let's try to express this sequence of operations in Java using integer division and remainder (modulus). In Java, we can express the quotient and remainder in long division as follows. 6 2)13 -12 1 Let's assume the initial decimal number is in variable n (an int) and let's refer to the digits of the result as (b3 b2 b1 b0)₂ = (1 101), (respectively). 2 Then 13/2 integer division yields the quotient b0-n2; b1 = (n/2) = 2; b2= ((n / 2) / 2) = 2; b3 (((n / 2) / 2) / 2 ) = 2; and b0 = n2; n-n / 2; b1 = n2; n-n / 2; b2 = n2; n-n / 2; b3 = n2; This is rather tedious as we keep having the calculate the new quotient using prior quotients. An easier way to do this is to change n to the most recent quotients as we go, //b0 will be 1 // new n will be 6 // bl will be 0 (since 6 // new n will be 3 13%2 modulus operator yields the remainder // b2 will be 1 (since 3 // new n will be 1 // b3 will be 1 (since 1 = 1 2 - 0) 2 - 1) 2 - 1)
(DecBin2) Write a program that will accept a number in decimal and display the number in binary. In Programming Assignment 2, you wrote DecBinl which converted and displayed a decimal number in binary for BUT your program was limited to decimal numbers between 0 and 15 due to the fixed number of bits being sought ( four). In this programming assignment we seek to remedy this limitation! (DecBin1) Consider the conversion of a non-negative decimal (base 10) integer to base 2 (binary). We will assume the non- negative base 10 integer has value between 0 and 15 inclusive. As a guiding example, let's convert (13), to binary (base 2). Using long division, we successively divide by the base to which we are converting, 2 in this case. 6 3 1 0 2)13 26 -12 6 0 101 1 The resulting binary number is (1101), (writing the remainders as read from right to left). Now let's try to express this sequence of operations in Java using integer division and remainder (modulus). In Java, we can express the quotient and remainder in long division as follows. 6 2)13 -12 1 Let's assume the initial decimal number is in variable n (an int) and let's refer to the digits of the result as (b3 b2 b1 b0)₂ = (1 101), (respectively). 2 Then 13/2 integer division yields the quotient b0-n2; b1 = (n/2) = 2; b2= ((n / 2) / 2) = 2; b3 (((n / 2) / 2) / 2 ) = 2; and b0 = n2; n-n / 2; b1 = n2; n-n / 2; b2 = n2; n-n / 2; b3 = n2; This is rather tedious as we keep having the calculate the new quotient using prior quotients. An easier way to do this is to change n to the most recent quotients as we go, //b0 will be 1 // new n will be 6 // bl will be 0 (since 6 // new n will be 3 13%2 modulus operator yields the remainder // b2 will be 1 (since 3 // new n will be 1 // b3 will be 1 (since 1 = 1 2 - 0) 2 - 1) 2 - 1)
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images