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:
- Start
- Define num,bnum,dec=0,base=1,rem
- Accept value of num
- bnum=num
- If num>0 go to Step 6 else go to Step 10
- Calculate rem=num%10
- dec=dec+rem*base
- num=num/10
- base=base*2
- Go to Step 5 again
- Print Binary Number
- Print Decimal Number
- Stop
No comments:
Post a Comment
Feel free to ask us any question regarding this post