My code:
#include <iostream>
#include<cmath>
using namespace std;
int main() {
int t;
cin>>t;
for(int k=1;k<=t;k++)
{
int base=2,n,n1,d,nd=0,i=0;//n=binary no.,nd=decimal quivalent of n
cin>>n;//binary no.
n1=n;
while(n1>0)
{
d=n1%10;
nd=nd+(d*pow(2,i));
i++;
if(i<16)//since there can be max 16 digits, power will go till 15
n1=n1/10;
}
cout<<nd<<endl;
}
return 0;
}
My output: Wrong Answer. !!!Wrong Answer
Possibly your code doesn't work correctly for multiple test-cases (TCs).
The first test case where your code failed:
Input: 11111111111
Its Correct output is: 2047
And Your Code's output is: 2559
Original question: Binary number to decimal number
Given a Binary Number B, 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 follow. Each test case contains a single Binary number B.
Output: For each testcase, in a new line, print each Decimal number in new line.
Constraints: 1 <= T <= 100 1 <= Digits in Binary <= 16
Example: Input: 2 10001000 101100 Output: 136 44