-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinaryToDecimal.cpp
More file actions
52 lines (43 loc) · 1.22 KB
/
BinaryToDecimal.cpp
File metadata and controls
52 lines (43 loc) · 1.22 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
Convert Binary Number to Decimal Number
*/
#include<iostream>
#include<math.h> //for function pow()
#include<string.h> //for function strlen()
using namespace std;
int main()
{
char str[20],temp;
int x;
int bintodec(char []);
cout<<"Enter a Binary Number : "<<endl;
gets(str);
int j=strlen(str)-1; //to calculate string length
for(int i=0;i<=j;i++,j--) //to reverse string elements so that 1's or 0's can
{ //be multiplied by correct powers of 2
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
x=bintodec(str); //function call to convert to decimal number
if(x!=-1)
cout<<"Decimal Equivalent : "<<x<<endl;
else
cout<<"It is not a binary number!!!"<<endl;
return 0;
}
int bintodec(char str[])
{
int num,dec=0;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]-'0'==0||str[i]-'0'==1) //to check if str[i] is 1 or 0
{
num=str[i]-'0';
dec=dec+num*pow(2,i);
}
else //if str[i] is neither 1 nor 0, then return -1
return -1;
}
return dec;
}