TechoSagar: Binary number to decimal number

Binary number to decimal number

Given a Binary Number, Print its decimal equivalent.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case contains a single Binary number. 
Output: 
Print each Decimal number in new line.
Constraints:
1< T <100
1<=Digits in Binary<=8
Example:
Input:
2
10001000
101100
Output:
136
44
Your Code
#include<iostream>

using namespace std;
#include<math.h>
int main()
{
int t;
cin>>t;
while(t--)
{
long int n;
int i=0, sum=0;
cin>>n;
while(n!=0)
{
int k;
k=n%10;
n=n/10;
sum=sum+k*pow(2,i);
i++;
}
cout<<sum<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook