Write a program to convert binary number to decimal number.
Submitted On | |
By | Cool_Gemini |
Level | Beginner |
User Rating | 4.0 (8 globes from 2 users) |
Compatibility | C, C++ (general), UNIX C++ |
Category | Algorithms |
World | C / C++ |
Archive File |
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(void)
{
int i=0,j=0,sum=0,n,tmp,tmp1;
long int arr[5];
char pch;
clrscr();
printf("Enter binary number : ");
while (scanf("%d",&n) != 1)
{
while ((pch = getchar()) != '\n')
putchar(pch); // dispose of bad input
printf(" is not an binary number.\nPlease enter a ");
printf("binary value, such as 100, 1011, or 1111: ");
}
while(n!=0)
{
arr[i]=n%10;
n=(n-arr[i])/10;
tmp=i;
i++;
}
if(i>=5)
{
printf("\n\tNumber Out Of Range");
getch();
exit(0);
}
for(j=0;j<=tmp;j++)
if(arr[j]!=1&&arr[j]!=0)
{
printf("Enter binary no. only");
getch();
exit(0);
}
tmp1=tmp;
for(j=0;j<=tmp;j++)
{
sum=sum+(arr[j]*(pow(2,tmp1)));
tmp1--;
}
printf("\nEquivalent decimal number is %d",sum);
getch();
return 0;
}