Write program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write program

I want help how to write the programming for this question (To convert 234 into binary number) ???

16th Mar 2019, 6:25 PM
Azza
7 Answers
+ 4
to convert a number to binary you can do it with the mod operator: 234 % 2 = 0 (234/2 = 117) 117 % 2 = 1 (117 / 2 = 58) 58 % 2 = 0 29 % 2 = 1 14 % 2 = 0 7 % 2 = 1 3 % 2 = 1 1 % 2 = 1 Now you have this series: 01010111 Reverse it to get the binary number: 11101010 = 234 * int number = 234 * int array to store the 0's and 1's * a for loop --> 8 bit --> i from 0 to 7 (highest 8 bit number = 255 = 11111111) * inside for loop: store number % 2 in your array, then number/=2 * at the end: reverse your array
16th Mar 2019, 7:05 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
The theory: https://www.google.de/amp/s/m.wikihow.com/Convert-from-Decimal-to-Binary%3famp=1 int n = 234; //your number int[] binary = new int[8]; //an array //fill your array for(int i = 0; i < 8; i++){ binary[i] = n % 2; n/=2; } //binary = [0,1,0,1,0,1,1,1] //print your array (reverse it) for(int i = 7; i >=0; i--){ System.out.print(binary[i]); } //output: 11101010
19th Mar 2019, 5:11 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
19th Mar 2019, 5:24 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thank you so much
16th Mar 2019, 7:16 PM
Azza
+ 1
Azza Your welcome.
16th Mar 2019, 7:20 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Denise Roßberg I'm really grateful to you
23rd Mar 2019, 3:45 AM
Azza
0
Excuse me can you explain more this question 🥺?? I try to write the programming in Code Blocks but i can't do it .. I'm a novice in programming and I have to solve it and submit it tomorrow! Denise Roßberg
19th Mar 2019, 4:57 PM
Azza