Decimal To Binary Conversion In Java

  Рет қаралды 94,723

The Programming Portal

The Programming Portal

7 жыл бұрын

In this video, you will learn Decimal to Binary conversion program In Java.
We will use Scanner class for user input and array to store and print decimal to binary output in java.
docs.oracle.com/javase/7/docs...
docs.oracle.com/javase/7/docs...)
In this video you will learn:
- how to take input from user in Java
- Scanner class in Java
- how to create object in java
- how to convert decimal to binary in java
- array in java
- nextInt method in java
- how to declare a variable in Java
- how to declare an array in Java
- for loop in java
- while loop in java
- how to use for loop for iteration in java
- if condition in java
Want to learn more from me?
All Conversion Playlist in Java :
• Decimal to Hexa Conver...
Java examples complete Playlist :
• Java Factorial Program
Python examples complete Playlist :
• python program for str...
#JavaTutorial
#JavaTutorialForBeginners
#JavaProgramToCovertDecimalToBinary
#DecimalToBinaryInJava
#DecimalToBinaryConversionInJava
#JavaExamples
#JavaConversionTutorial
#ArrayInJava
#ScannerclassInJava
#ForLoopInJava
Subscribe to my other channel for random videos:
IndianBox : / @indianbox1736
Follow us on:
Facebook : / theprogrammingportal
Instagram : / theprogrammingportal
Support:
Patreon : / theprogrammingportal
Keep Coding!

Пікірлер: 71
@yogesh7225
@yogesh7225 5 жыл бұрын
I do not understand why you added an empty string to each value of binary array. It seems to work fine without it as well.
@ilyasghziel5888
@ilyasghziel5888 3 жыл бұрын
Thank you man! you helped me a lot!
@TheProgrammingPortal
@TheProgrammingPortal 3 жыл бұрын
Glad it helped you. Do subscribe and check out other videos which might be helpful for you.
@thebhagwabilla
@thebhagwabilla Жыл бұрын
thanks sir. very useful for me.
@anuragvc1995
@anuragvc1995 5 жыл бұрын
short and simple.
@asadmhagne2897
@asadmhagne2897 5 жыл бұрын
you are my HERO thx man now my teacher won't kill me after all !! you have earned a Subscriber !!
@TheProgrammingPortal
@TheProgrammingPortal 5 жыл бұрын
asad mhagne glad it helped you. Keep learning.
@hartnathan97
@hartnathan97 Жыл бұрын
How would one write this not using an array list but a while loop.
@mohamedahmed-vf9wo
@mohamedahmed-vf9wo 4 жыл бұрын
can you help me write tis code Design a GUI program that accepts inputs from the user as negative unsigned number in the decimal system that is required to find its binary representation based on (1) Sign magnitude system (2) one’s complement (3) two’s complement. please
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
Hi, you can use Scanner class for taking input and allows some option to calculate using different methods using switch case or conditional statements. I suggest you try few codes by own and will help you with any error or issue. Find below link to understand some terms and write base logic. stackoverflow.com/questions/35278819/sign-magnitude-ones-complement-twos-complement www.electronics-tutorials.ws/binary/signed-binary-numbers.html This will help your learning.
@vvbhargav5854
@vvbhargav5854 Жыл бұрын
nice bro
@intiiit.babe16
@intiiit.babe16 4 жыл бұрын
thank u so much
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
ImAkward 1999 glad it helped you.
@intiiit.babe16
@intiiit.babe16 4 жыл бұрын
yes, but how can i make a flowchart with this? pls teach me
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
ImAkward 1999 if you want to design flow chart diagram for this logic. You can use google gliffy tool and add conditional and loop in same for this program.
@brian7195
@brian7195 5 жыл бұрын
thankyou my friend u saved my ass boy
@TheProgrammingPortal
@TheProgrammingPortal 5 жыл бұрын
bucika calaka glad it helped you. Keep supporting.
@toeoowaiyan7546
@toeoowaiyan7546 4 жыл бұрын
good
@nateh254
@nateh254 4 жыл бұрын
Hello you probably won't see this, but when I try to do this, it tells me that "local variable i has not been initialized" and I don't know how to solve this. I even tried copying your code line for line and the error is still there.
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
Hey, your error showing i not intialized? In video i value has set to 0. Please review your code once and check if i is accessible from function or line where you using it. Suggest you to go once in entire video and paste code here if still facing the issue Will help you as earliest.
@gudiyachaudhary8235
@gudiyachaudhary8235 5 жыл бұрын
Sir actually I have my computer practical exam tomorrow on 18th feb 2019 n our teacher doesn't allow us to use SCANNER and Array please reply me with the non scanner n non Array form of the same program sir please I will rlly be grateful to u please do that as soon as possible....please with the output as well...
@TheProgrammingPortal
@TheProgrammingPortal 5 жыл бұрын
Smita Chaudhary hey, if scanner class in not allowed- simply declare a variable with some values..i.e. no user input require. Eg: int decimalNumber =10; or u can usr readLine() of bufferReader or Console class if you want user should give input. 2nd without using array - many different ways available. 1st - use binaryOutput as string and every next time you can append output value in that string. (Not checked but it can work) 2nd - use Integer.toString(decimalNumber,2); // 2 is base of binary system 3rd - Integer.toBinaryString(decimalNumber); These are inbuilt function without user defined logic. If you want to use ur logic based, use method 1 of using string and appending.
@gudiyachaudhary8235
@gudiyachaudhary8235 5 жыл бұрын
Thank u sir...Thanks a lot
@TheProgrammingPortal
@TheProgrammingPortal 5 жыл бұрын
Smita Chaudhary glad I was able to help you. Do subscribe for channel for support and upcoming video's and share within your colleagues. 😊
@Ambi2004
@Ambi2004 2 жыл бұрын
Can you please explain why you increased i value by 1??
@TheProgrammingPortal
@TheProgrammingPortal 2 жыл бұрын
i denotes index for an array. As for every digit we calculate binary and that binary value we store in array. Eg. binary[0]= 0 or 1 binary[1]= 1 or 0 ... And so on. i+1 help to store output in next index for array i.e. binary [ i ] Hope this helps you.
@akshaydwivedi935
@akshaydwivedi935 6 жыл бұрын
can we do it without using array?
@TheProgrammingPortal
@TheProgrammingPortal 6 жыл бұрын
jack ryder yes we can. One of the way is you can store values in string where array value getting stored and concatenate that string value in every loop iteration, but it will print binary in opposite order which is incorrect, so you have to print that string in reverse order using inbuilt functions of some sort of logic.
@CASPER28x
@CASPER28x 4 жыл бұрын
i need some help here :( binary[i]=number (here) 2; what this thing here
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
Hi, as decimal to binary i.e. converting from base10 to base2 binary[I] = number%2 will returns with mod i.e. reminder which either it be 0 or 1 as we using by number 2 This all results will store in array and then same array is printed in opposite direction.
@CASPER28x
@CASPER28x 4 жыл бұрын
@@TheProgrammingPortal ok thx bro ✨
@royjustinevirtudazo8825
@royjustinevirtudazo8825 3 жыл бұрын
Can someone explain to me what is 100 in line 12 ? :
@TheProgrammingPortal
@TheProgrammingPortal 3 жыл бұрын
Hey 100 is a size of int array.
@mohanasundari6188
@mohanasundari6188 4 жыл бұрын
Sir i didn't understand for loop (need) explain me sir
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
Hey, we have stored all binary values 0's & 1's inside array -> binary [ ] using while loop. Now comes to ur query for loop - To display the output as we have stored same in array, we need to read each element of array one by one using index and display. Array should be read from last index to first index in reverse order i.e. last index->0. ( Binary output stored in such way ). Eg. Binary [0,1,1,0,1] Output: 10110 (print each element of array) You can use string instead of array and concatenate each output to string and display whole string as output. Hope it clarifies your query. Do let me know if needed more.
@RahulGupta-pg3lc
@RahulGupta-pg3lc 4 жыл бұрын
Hey if I put 0 it's showing incorrect ans
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
Hey Rahul in this program input should be > 0. If you want you can add single line to check 0 constraint and return 0 for its binary value.
@sayantanchakraborty491
@sayantanchakraborty491 6 жыл бұрын
good programming
@TheProgrammingPortal
@TheProgrammingPortal 6 жыл бұрын
Sayantan Chakraborty thanks for your feedback sir.
@RahulGupta-pg3lc
@RahulGupta-pg3lc 4 жыл бұрын
0 can't convert into 0
@prasuguna
@prasuguna 3 жыл бұрын
why printing in reverse?
@TheProgrammingPortal
@TheProgrammingPortal 3 жыл бұрын
Hi, Binary numbers always have right to left direction.. eg. 12--> 1100.. so first value is 0.. second is 0.. third is 1..fourth is 1. by calculating we store our value as 0011 in array.. so while printing results we perform reverse in this case printing array value in reverse order to get correct result. Hope this helps you. You. Can try on pen and paper.
@intiiit.babe16
@intiiit.babe16 4 жыл бұрын
can someone provide a flowchart for this? 😭
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
ImAkward 1999 download Google gliffy free extension on chrome and you can add condition loops and can design it very easily.
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
ImAkward 1999 find below link to see how it works - www.google.com/amp/s/www.gliffy.com/blog/how-to-create-flowcharts-with-gliffy%3fhs_amp=true
@intiiit.babe16
@intiiit.babe16 4 жыл бұрын
thank you so much, you are a big help
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
ImAkward 1999 do subscribe share and check other videos
@beehive7753
@beehive7753 3 жыл бұрын
Sir, Im kinda lost because my prof. wanted it to look like 10 digits ex. 7 decimal 000001001 how do I do that?
@TheProgrammingPortal
@TheProgrammingPortal 3 жыл бұрын
hi, I am assuming you want to append zeros to fit binary output of length 10 instead of printing only exact length binary output. yes, you can append values to array at initial position and shift remaining array elements i.e. binary output towards right side. use length to determine how much shifting to be done.
@beehive7753
@beehive7753 3 жыл бұрын
@@TheProgrammingPortal I'm new to coding can I see what you mean ?
@TheProgrammingPortal
@TheProgrammingPortal 3 жыл бұрын
@@beehive7753 is it what you want --> binary of number 7 ==> "0000000111" instead of just "111" ?? binary of number 10 ==> "0000001010" instead of "1010"
@beehive7753
@beehive7753 3 жыл бұрын
@@TheProgrammingPortal yes sir thats what the output should be.
@TheProgrammingPortal
@TheProgrammingPortal 3 жыл бұрын
@@beehive7753 hey this can be done in two ways. 1.add leading zeros to array and Shift binary output to right side i.e. adding and shifting array elements. ( This is applicable if you using code from video but shifting array could be little complicated for you as you are new to coding.) SO USE SECOND METHOD 2. Istead of using array, you can use string method and add zero's using formattor. ( this is other method to convert decimal to binary not what explained in video - source code is changed) Source code below : import java.util.Formatter; String binary =""; int n = 10; while(n>0){ binary+= n%2; n = n / 2; } //Output need to printed in reverse order so using string builder StringBuilder sb=new StringBuilder(binary); // string to string builder conversion binary = sb.reverse().toString(); // reversing the string builder and converting back to string System.out.println("Without Formatted Output:"+ binary); // output without leading zero's String formatted_binary = String.format("%010d", Integer.parseInt(binary)); // using formattor to add zero's //if your output length is 20 then add "%020d" if 15 then add "%015d" System.out.println("Formatted Output with leading zeros:"+ formatted_binary); Hope this helps you.
@anup161091
@anup161091 4 жыл бұрын
why j=i-1???
@TheProgrammingPortal
@TheProgrammingPortal 4 жыл бұрын
In the given example, i has been used to store the value in array starting from 0th index. I am printing the array in reverse order so i= j-1 gives last index of array.(I get incremented by 1 post evry condition check in while loop) You just have to set last index if you want to print reverse array. Same can done by via different other methods as well. Hope it clarifies your doubt.
@shrutishah4690
@shrutishah4690 5 жыл бұрын
Why we use loop
@TheProgrammingPortal
@TheProgrammingPortal 5 жыл бұрын
Shruti Shah we use while loop here such that every time a binary number is generated from the number, number value should be divided and it iterates until it comes to zero and stops. Other loop I used to print binary values bcz I have stored each binary output in array and printing each array values. You can print binary output directly without using of 2nd loop.
@shrutishah4690
@shrutishah4690 5 жыл бұрын
Ohk
@TheProgrammingPortal
@TheProgrammingPortal 5 жыл бұрын
Shruti Shah do let me know any more explanation is required. Keep learning.
@itswanadiaries
@itswanadiaries 2 жыл бұрын
it says error for me 😭😭
@TheProgrammingPortal
@TheProgrammingPortal 2 жыл бұрын
Can you recheck the code it should work as expected. Please share error if any here
@itswanadiaries
@itswanadiaries 2 жыл бұрын
@@TheProgrammingPortal yes please :
@itswanadiaries
@itswanadiaries 2 жыл бұрын
@@TheProgrammingPortal import java.util.Scanner; public class DecimalToBinary { public static void main(String[] args) { int number,i; Scanner user_input=new Scanner(System.in); System.out.print("Enter decimal number :"); number=user_input.nextInt(); while (number!=0) { binary[i]=number%2; number=number/2; i++; } System.out.print("Binary value is :"); for(int j=i-1>0;j--; { System.out.print(""+binary[j]); } } }
@itswanadiaries
@itswanadiaries 2 жыл бұрын
@@TheProgrammingPortal and my decimal is 1947.9 im cryinggg rn 😭😭
@TheProgrammingPortal
@TheProgrammingPortal 2 жыл бұрын
As current program doesn't support fraction value conversion, you can apply algo for fraction calculation. 1.split the number to integral and fraction part 2. Calculate binary for integer as per program in current video 3. Get the decimal part 4. multiple by 2 to get some number 5. Take the integral part from multiplication output and consider as binary 0 or 1 6. Follow step 4,5,6 again on fraction part until the multiplication results not zero. 7. Stop if multiplication value reaches to zero or set some precision to how much you need to calculate let's upto 5 decimal - number reach to zero condition will not be achieved easily and it may go to infinite or long output, setting precision count is helpful. 8.join fraction binary to int binary In your example: calculate for 1947 then for 9 0.9*2=1.8 Read 1 as binary and rerun for 0.8 And multiple by 2 0.8*2=1.6 Save 1 as binary and follow same for 0.6 until it reaches zero or reaches to your precision count. Hope this helps.
@rachaitalokhande2350
@rachaitalokhande2350 Жыл бұрын
explain bhi kr liya kr jyada smjhe ga
@justinjonathanbertram1544
@justinjonathanbertram1544 6 жыл бұрын
BRUH
Binary To Decimal Conversion In Java
11:43
The Programming Portal
Рет қаралды 12 М.
Java code for Converting Decimal Number to Binary Number
14:50
TrainingMug
Рет қаралды 13 М.
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 36 МЛН
Son ❤️ #shorts by Leisi Show
00:41
Leisi Show
Рет қаралды 8 МЛН
How To Convert Decimal to Binary
13:24
The Organic Chemistry Tutor
Рет қаралды 4,4 МЛН
How do computers read code?
12:01
Frame of Essence
Рет қаралды 3,1 МЛН
Learn Java in 14 Minutes (seriously)
14:00
Alex Lee
Рет қаралды 4,6 МЛН
Convert Decimal Number to Binary  - Java Code
6:59
Programming Tutorials
Рет қаралды 4,7 М.
How To Convert Binary To Decimal - Computer Science
12:51
The Organic Chemistry Tutor
Рет қаралды 3,3 МЛН
Call by Value and Call by Reference in Java
5:44
Knowledge Circle
Рет қаралды 12 М.
If Else Statement In Java Tutorial #17
7:39
Alex Lee
Рет қаралды 321 М.
How to make advanced image recognition bots using python
15:01
Kian Brose
Рет қаралды 1,3 МЛН
小蚂蚁被感动了!火影忍者 #佐助 #家庭
00:54
火影忍者一家
Рет қаралды 36 МЛН