Answer to Question #16510 in C++ for Ashish

Question #16510
Problem 2:

Define your own function named TenToBinary, which will convert
a base-10 integer to a base-2 integer. The following steps are
required to be implemented in your program: (1) In your main
function, need to read an arbitrary input integer (nonnegative)
from the keyboard; (2) pass the input integer to your function (3)
convert the input integer which is based-10 to base-2; and (4)
print the binary value (base-2) in your own function. An example
is given as follows, which should be the format of your program
output on the console window (screen) after the execution.
we have to write this in c programming.
1
Expert's answer
2012-10-16T09:41:29-0400
#include <iostream>

using namespace std;




void show_binary(unsigned int u)

{

while(u > 1)

{

if((u % 2) == 0)

{

std::cout << "0";

}

else

{

std::cout << "1";

}




u = u >> 1;

}

std::cout << "1" << std::endl;

}




int main(){

int number = 142;

show_binary(number);

system("PAUSE");

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS