-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP46FUNCTION.cpp
More file actions
44 lines (36 loc) · 836 Bytes
/
P46FUNCTION.cpp
File metadata and controls
44 lines (36 loc) · 836 Bytes
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
// Convert Binary to Decimal
#include <iostream>
using namespace std;
int binaryToDecimal(int n)
{
int ans=0;
int x=1;
while (n>0)
{
int y=n%10; // getting last digit val
ans +=x*y; // x is basically denoting 2^0,2^1....
x *=2; // updating x
n /= 10; // to neglect decimal part and the second part becomes the last digit
}
return ans;
}
// int binaryToDecimal(int n)
// {
// int i=0;
// int ans=0;
// while (n>0)
// {
// int y=n%10;
// ans +=y*pow(2,i);
// i++;
// n /= 10;
// }
// return ans;
// }
int main(){
int n;
cout<<"enter a number:";
cin>>n;
cout<< binaryToDecimal(n)<<endl;
return 0;
}