+ 4
I need how to convert decimal to binary😊😊
8 Antworten
+ 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;
}
+ 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( );
}
+ 1
Divide the number by two and take the 1's and 0's from above to the beggining of the division
+ 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;
}
+ 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 \/
+ 1
what we think now.
0
dividing the given number by two🎓
- 2
you have to learn operations on bits .