array decToBin

hi, im wondering how can i make an array that converts a decimal to binary. thanks. :-)
@nicole Yoonzy

Create an array with each binary value. int binary_value[8]=(128,64,32,16,8,4,2,1); Also a null string variable, to hold '0' and '1's. ie:string Bin_num=""; This will let you convert numbers up to 255. To go higher, increase the the array to 16, and add more values, doubling as you go, as in the above. The ask for an integer number, and do a for loop. Check if the number is greater than the first number in your array. If yes, have the string variable to equal itself plus a "1", and subtract the value of binary_value[x] from the inputted decimal value. If not, a "0" is added to the string variable, instead. Of course, you'll have to set the limits on the inputted value. 255 with the above, or 65535, if you double the array size. Then print out the string variable, when the loop ends.

Hope this helps.

@whitebite1 thanks, but i was thinking of like you just have to input your number. for example ill input the number 10 then the output should be 1010 right? :-)
@nicole Yoonzy

input the number 10 then the output should be 1010 right? :-)


Yes, it would be. There may be a header you could include, that would do that, though I don't know which one that might be, but I gave the answer based on your original post.
make an array that converts a decimal to binary


Best of luck...
@whitenite1 thanks again. :-)
Topic archived. No new replies allowed.