0

# binary to decimal

Input :binary number. Output:Its eqivalent decimal. ex: Input:1001. Output:17

20th Sep 2017, 5:44 PM
+ 1
Sorry.
20th Sep 2017, 6:34 PM
David
+ 1
Please post your code, then I can see where you did mistakes.
21st Sep 2017, 5:00 AM
Jonas Schröter
+ 1
So, i saw you tried it, and because I don't understand everything in your code (no c++ expert) I'll give you my code. If you have questions, ask. https://code.sololearn.com/cmySYgxy5Bkl/?ref=app
21st Sep 2017, 5:44 AM
Jonas Schröter
0
I want code in java
20th Sep 2017, 6:32 PM
0
Look, to get the decimal value of a binary number, you use this formula: i = index n = number at i n × Math.pow(2, i); //You have to sum the value of all calculations Like: binary:101 decimal : 1 * Math.pow(2, 0) + 0 * Math.pow (2, 1) + 1 * Math.pow(2, 2) So, if you want to convert, you have to find a way to take the number of each index and calculate like above.
20th Sep 2017, 7:11 PM
Jonas Schröter
0
Then,how to take each number from that given number.And the input is given dynamically.
20th Sep 2017, 7:12 PM
0
so, at first, i would turn the binary String like 10011 --> 11001 Then you take a for loop starting at 0; Because you turned the String, you can use theString.charAt (i), i would be the variable of the for loop.
20th Sep 2017, 7:40 PM
Jonas Schröter
0
Can you write the code?
21st Sep 2017, 2:56 AM
0
Ur logic is wrong. The actual logic is if n=1001;y=1;dec=0; for(n!=0) { x=x%10; dec=dec+2*y; n=n/10; y=y*y; }
21st Sep 2017, 3:09 AM
0
No, my logic is correct. I've got a working code, but if I would give you a code that you don't understand, it doesn't help.
21st Sep 2017, 4:27 AM
Jonas Schröter
0
I've got a question, in your posted code there's a variable named x, where do you create it and where you assign a value to it the first time?
21st Sep 2017, 4:29 AM
Jonas Schröter
0
i explained only logic not the answer .Using that logic we can write the code for my problem.
21st Sep 2017, 4:41 AM
0
I know how to do that. I gave you ideas how to start off, so write a code. It's not important if it's working, but first, try it yourself. I don't want to throw a code into this thread and you don't understand it.
21st Sep 2017, 4:43 AM
Jonas Schröter
0
ok.if your code was running succesful .please post the code in comment.
21st Sep 2017, 4:43 AM
0
Don't you understand me?
21st Sep 2017, 4:45 AM
Jonas Schröter
0
i tried your logic but not working at all.so, i create a logic its working. This is my intension.
21st Sep 2017, 4:46 AM
0
Why you think imy logic isn't working?
21st Sep 2017, 4:48 AM
Jonas Schröter
0
Using with your logic, i write a code and after that i execute that code .finally there is no output . if, your was succesfully executed .put in your code in comment and i corrected where i gonna do mistakes in that code.
21st Sep 2017, 4:54 AM