Question: Here is my code in C programming: #include int main() { long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0; scanf(%ld,

Here is my code in C programming:

#include

int main() { long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0; scanf("%ld", &num); decimal_num = num; if(num>=0){ while (num > 0) { remainder = num % 2; /* To count no.of 1s */ if (remainder == 1) { no_of_1s++; } binary = binary + remainder * base; num = num / 2; base = base * 10; } printf("%ld ", decimal_num); printf("0b%ld ", binary); }else{ num = -num; while (num > 0) { remainder = num % 2; binary = binary + remainder * base; num = num / 2; base = base * 10; } char c[33]; int i=0; for(i=0;i0){ remainder = binary%10; if(remainder==1){ remainder = 0; }else{ remainder =1; } if(addRemain==1){ if(remainder==1){ remainder = 0; addRemain = 1; }else{ remainder = 1; addRemain = 0; } } if(first==1){ if(remainder==1){ remainder = 0; addRemain = 1; }else{ remainder = 1; addRemain = 0; } } c[count] = '0'+remainder; binary = binary/10; count--; first = 0; } printf("0b%s",c); } return 0; }

for the following question:

Here is my code in C programming: #include int main() { long

num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0;

_______________________________________________________________________________________________________

This is what happens when I run the code:

scanf("%ld", &num); decimal_num = num; if(num>=0){ while (num > 0) { remainder

where

"./dec2bin 5

12"

is the input and output is

"12

0b1100"

_______________________________________________________________________________________________________

But the actual input and output needs to look like:

= num % 2; /* To count no.of 1s */ if (remainder

_______________________________________________________________________________________________________

The right output should only include the "0b01100" and not an additional "12" above it. Also the binary output is wrong.

Can someone please finx my code? Thank you.

Write a program that takes an integer from stdin, and then prints the binary representation (with a leading eb to denote the format is binary). Negative numbers should be represented in two's complement. The total bit length is given as the first argument to your program, and leading zeros should not be truncated You may assume that input is valid (i.e. you do not need to validate input), and the number given is always within the range of the bit length required to represent the number. You may also assume that the maximum bit length given ill be 32 Examples Text ./dec2bin 5 12 0b01100o Text ./dec2bin 4 5 0b0101 Text ./dec2bin 3 ob011 Text ./dec2bin 3 0b000 Text ./dec2bin 3 0b101 Text ./dec2bin 32 256

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!