I need how to convert decimal to binary😊😊 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

I need how to convert decimal to binary😊😊

29th Oct 2016, 9:37 AM
Atul Anand
Atul Anand - avatar
8 Answers
+ 5
you need to get the input from the user,intialize the binary number as 0.keep on dividing the number by 2 for obtaining remainders as 1 or 0,store them in your binary variable by continuously multiplying in a loop keep updating the input number by dividing it by 2.do this until the value of your number becomes 0.use a while loop for this heres your code: #include<iostream> using namespace std; int main() { int n; cout<<"enter a number:\n"; cin>>n; cout<<"the binary equivalent is:" int b=0,r; while (n!=0) { r=n/2; n=n/2; b=b*10+r; } cout<<b; return 0; }
29th Oct 2016, 4:35 PM
Saptarshi Saha
Saptarshi Saha - avatar
+ 2
#include<stdio.h> #include<conio.h> void main( ) { int a[15],n,i, j,d; clrscr( ); printf("enter the number"); scanf("%d",&n); i=0; while(n!=0) { a[ i ]=n%2; n=n/2; i++; } for(j=i;j>=0;j--) printf("%d",a[j]); getch( ); }
29th Oct 2016, 4:17 PM
Tarun Chaudhary
Tarun Chaudhary - avatar
+ 1
Divide the number by two and take the 1's and 0's from above to the beggining of the division
29th Oct 2016, 10:22 AM
Pablo RG
Pablo RG - avatar
+ 1
You may make use of boolean array to store the numbers. I assume your numbers fit in a 32-bit integers range. #include<iostream> int main () { int num; cin>>num; bool binary[32]; for(unsigned int i=1,j=0;j<=32;i*=2,j++) binary[j]= num&i; for(int i=32;i>=0;i--) cout << binary[i]; return 0; }
29th Oct 2016, 1:17 PM
Hayden Lee
+ 1
The method of converting Decimal to Binary is repeated division method.In this method, the number is successively divided by 2 and its remainders recorded.The final binary results is obtained by assembling all the reminders,with the last remainders being the most significant bit(MSB) FOR EXAMPLE: 2 | 43. 1 2 | 21. 1 2 | 10. 0 2 | 5. 1 2 | 2. 0 2 | 1. 1 | 0. 1 WRITE THE ORDER FROM DOWN TO UP^ Hope this will help you PEACE \/
21st Dec 2016, 7:57 PM
Usman khan
Usman khan - avatar
+ 1
what we think now.
30th Jan 2017, 11:48 AM
Ankur Soni
Ankur Soni - avatar
0
dividing the given number by two🎓
29th Oct 2016, 10:11 AM
sameer Shaikh
sameer Shaikh - avatar
- 2
you have to learn operations on bits .
17th Nov 2016, 5:20 PM
D E E P A N S H U YADAV
D E E P A N S H U YADAV - avatar