Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

image text in transcribed

_______________________________________________________________________________________________________

This is what happens when I run the code:

image text in transcribed

where

"./dec2bin 5

12"

is the input and output is

"12

0b1100"

_______________________________________________________________________________________________________

But the actual input and output needs to look like:

image text in transcribed

_______________________________________________________________________________________________________

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What performance issues are there in the company?

Answered: 1 week ago