Sem 5‎ > ‎DCCN LAB‎ > ‎

P-0 WAP to Convert Decimal to Binary Number

posted Nov 6, 2012, 12:18 AM by Neil Mathew


Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream.h>
#include <conio.h>
void main()
{
     clrscr();
  int n;
 
  cout<<"Enter A Decimal Number: ";
  cin>>n;
 
  cout<<"\n DECIMAL: "<<n;
 
        // 128 = 2^7 = 8 bit
        //  64 = 2^6 = 7 bit
        //  32 = 2^5 = 6 bit
        //  16 = 2^4 = 5 bit
        //  08 = 2^3 = 4 bit
        //  04 = 2^2 = 3 bit
        //  02 = 2^1 = 2 bit
        //  01 = 2^0 = 1 bit
 
  cout<<"\n BINARY: ";
 
  for (int i = 128; i != 0; i=i>>1)
  {
    if (n & i)
      cout<<"1 ";
    else
      cout<<"0 ";
  }
 
  cout<<endl;
  getch();
 


Output:

Enter A Decimal Number: 12
 DECIMAL: 12
 BINARY: 0 0 0 0 1 1 0 0 
Comments