29 AnswersNew Answer
bcd of a decimal no.(Binary-Coded-Decimal) eg: 167 bcd eqivalent-> 0001 0110 0111 binary eqivalent-> 10100111 TASK--> if number of 1's in binary is equal to number of 1's in bcd ''upto that length''of binary (from right to left) print the number, print bcd eqiv, print binary eqiv.. eg: 18---bcd 00011000 ( two 1's) 18--binary 10010 ( two 1's) eg: 179---bcd 0001 01111001 ( five 1's) 179--binary 10110011 (five 1's) # REMEMBER...UPTO BINARY LENGTH
8/24/2017 6:49:30 PMsayan chandra
29 AnswersNew Answer
179---bcd 0001 0111 1001 ( six 1's) 179--binary 10110011 (five 1's)
Here's mine📝 https://code.sololearn.com/caqALbNPAJlk/?ref=app
this is my code in Python.. It checks every number from 10 to the given input (the numbers from 1 to 9 obviously have this property because bin and bcd representations are equal) https://code.sololearn.com/cKajQ9vjyGg6/?ref=app
SURVEY UPDATE---> @ vari93 @baptiste @ ysraelcon @sam0delk1n @prudhbi raju did everything perfect...with explaination. @seung @m abrate also as i wanted but try doing your function.. not predefined..but its ok
mine https://code.sololearn.com/WdOjtYzkfhD9/?ref=app still thursday on here :) note: I think 179 isn't, ah, depending of the binary's length it is, I got it, fixed 👌
Here's my C# implementation ✌ LINQ One-Liner〰 strNumBinary.Count(c => c == '1') == strNumTrimmedBCD.Count(c => c == '1') Unfortunately my program hit the limit of 300+ as it's a naive approach without optimization. I'll try to improve it when got time later. Enjoy! https://code.sololearn.com/ciI5ixbJDa2K/?ref=app
I did not had the time to do it earlier ... My input starts at 10 and not 1 as numbers from 1 to 9 will always be printed, they have no interest edit : work with the binary length rule now https://code.sololearn.com/cG5tF5JqdAX7/?ref=app
predefined? do you mean avoid format() and bin()?
rewrote without bin(), format(), join(), range()
numbers start from 0 to inputs ? from 9 to max of the data type ? (like unsigned)
> eg: 167 > bcd eqivalent-> 0001 0110 0111 > binary eqivalent-> 1000111 Something wrong with your binary equivalent. 1000111b = 71d 167d = 10100111b
dec bcd bin / only count 1s in bcd up to len(bin) https://code.sololearn.com/cNPveOvum2ad/?ref=app
In the example of 179, bin has 8 digits '1011 0011'. And sayan only counts 1s in the last 8 digits of bcd '0111 1001', ignoring the first 4 digits '0001'. At least, that is how I understood and programmed my code.
I guess "upto max limit (input)" in the title means all numbers from 1 to input
https://code.sololearn.com/c9wAv9DRPSUn/#cpp @sayan, now it works on all numbers in range.
This sounds fun, I'll work on it when I get out of class
Alright, here you go lol. Finally got it to work, I was originally doing comparisons with literal binary representations in bcd, I wasn't adding 0s. Thank you Seung Moo. https://code.sololearn.com/ciL9PO3acX9t/?ref=app
I think only one number needs print out, not all from 1 to N, see the first post.
SoloLearn Inc.4 Embarcadero Center, Suite 1455
Send us a message