Tuesday 5 February 2013

C program to convert a decimal number to a binary number with output and proper explanation.

C program to convert a decimal number to a binary number with output and proper explanation.


  1. # include <stdio.h>  
  2. # include <conio.h>  
  3. void main()   
  4. {   
  5.  long b[20], n, r, c = 0, i ;   
  6.  clrscr() ;   
  7.  printf("Enter a decimal number : ") ;   
  8.  scanf("%ld", &n) ;   
  9.  while(n > 0)   
  10.  {   
  11.   r = n % 2 ;   
  12.   b[c] = r ;   
  13.   n = n / 2 ;   
  14.   c++ ;   
  15.  }   
  16.  printf("\nThe binary equivalent is : ");   
  17.  for(i = c - 1 ; i >= 0 ; i--)   
  18.   printf("%ld", b[i]) ;   
  19.  getch() ;   
  20. }   

Output of above program is

Enter a decimal number : 18 
The binary equivalent is : 10010

How to convert Decimal Number to Binary form? The basic method for this conversion is -
  • Divide the decimal number by 2. Note only the integer part of quotient. Also note down the remainder i.e. if the number is divisible by 2 then 0 otherwise 1.
  • Repeat the above step for every quotient until the quotient becomes 0.
  • When quotient becomes zero read the remainders in reverse direction to get the binary form of your number.
Decimal to Binary Conversion


Explanation of above program

First take a look at all the variables. All variables are of Long Data Type- 
  • n - is the number that user enters.
  • b[20] - is an array of size 20 and is used to store the remainders obtained along the calculation. To obtain the binary number once the calculation is finished we just have to reverse this array.
  • r - is used to store the remainder temporarily.
  • i - is the loop variable.
  • c - is a counter variable which is initialized to zero at the start of the program.
First we ask the user to enter a number. Then using while loop we are calculating the remainders and storing it in the array b[20]. After that using for loop we reverse our array and finally prints the binary equivalent of the entered number.

Suppose, n = 18 - 

Working of while loop -

n > 0 i.e. 18 > 0. While loop is executed -
  • r = n % 2 = 18 % 2 = 0.
  • b[c] = r or b[0] = 0.
  • n = n / 2 or n = 18 / 2 = 9.
  • c++ or c = 1.

n > 0 i.e. 9 > 0. While loop is executed -
  • r = n % 2 = 9 % 2 = 1.
  • b[c] = r or b[1] = 1.
  • n = n / 2 or n = 9 / 2 = 4. (only integer part)
  • c++ or c = 2.

n > 0 i.e. 4 > 0. While loop is executed -
  • r = n % 2 = 4 % 2 = 0.
  • b[c] = r or b[2] = 0.
  • n = n / 2 or n = 4 / 2 = 2.
  • c++ or c = 3.

n > 0 i.e. 2 > 0. While loop is executed -
  • r = n % 2 = 2 % 2 = 0.
  • b[c] = r or b[3] = 0.
  • n = n / 2 or n = 2 / 2 = 1.
  • c++ or c = 4.

n > 0 i.e. 1 > 0. While loop is executed -
  • r = n % 2 = 1 % 2 = 1.
  • b[c] = r or b[4] = 1.
  • n = n / 2 or n = 1 / 2 = 0.
  • c++ or c = 5.
n is not greater than 0. So while loop terminates. Now the values are -
  • n = 0.
  • c = 5.
  • b[0] = 0, b[1] = 1, b[2] = 0, b[3] = 0 and b[4] = 1
Now in order to get our binary number we just have to print the values in array b[20] in reverse direction. The for loop in our program serves this purpose.

No comments:

Post a Comment