Simple C Program to Convert Binary to Decimal Number


Using this program user can  enter binary number and convert it into decimal 
Binary number system:-It is base 2 number which uses the digits from 0 and 1.
 
Decimal number system:-It is base 10 number which uses the digits from 0 to 9

   For example we want to convert binary number 0010100 to decimal:
Step1:  0 * 1  = 0
Step2:  0 * 2  = 0
Step3:  1 * 4  = 4
Step4:  0 * 8  = 0
Step5:  1 * 16 = 16


Its decimal value: 0 + 0 + 4 + 0 + 16 = 20

 
Program:
 
 
//C Program to enter binary number and find out its decimal by www.SlashMyCode.com
#include <stdio.h>
#include <conio.h>
void main()
{
    int num,bnum,dec=0,base=1,rem;
    printf("Enter Binary Number(1s and 0s)\n");
    scanf("%d",&num);
    bnum=num;
    while(num>0)
    {
        rem=num%10;
        dec=dec+rem*base;
        num=num/10;
        base=base*2;
    }
    printf("binary num=%d\n",bnum);
    printf("Decimal Number=%d",dec);
     //C Program to enter binary number and find out its decimal by www.SlashMyCode.com
    getch();
}
Output:
 

Algorithm:
  1. Start
  2. Define num,bnum,dec=0,base=1,rem
  3. Accept value of num
  4. bnum=num
  5. If num>0 go to Step 6 else go to Step 10
  6. Calculate rem=num%10
  7.                 dec=dec+rem*base
  8.                 num=num/10
  9.                 base=base*2
  10. Go to Step 5 again
  11. Print Binary Number
  12. Print Decimal Number 
  13. Stop

No comments:

Post a Comment

Feel free to ask us any question regarding this post