0

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

Bhavana
  • 11
  • 1
  • 4
  • 5
    Did you step-through your code line-by-line in a debugger? – Happy Green Kid Naps Jun 08 '20 at 04:29
  • 2
    You might want to do some testing on your own, especially when you know which input to focus on. A good place to start is to verify your data. After every read from `cin`, stream the value to `cout` to confirm the value is what you think it is. (If you're still stuck, please hardcode values in your [mre], rather than relying on us to enter something sensible when we run the program.) – JaMiT Jun 08 '20 at 04:36
  • 2
    Check the following. Maximum value of `int` in your environment greater than "11111111111". Double type of `pow()` getting in the way ( https://stackoverflow.com/questions/588004/is-floating-point-math-broken ). – Yunnosch Jun 08 '20 at 04:43
  • 4
    Recommendation: Don't take `n` as an `int`. Take it as a `std::string`. You have nearly infinite length and it's really easy to take apart: No math required. You reverse-iterate the characters in the string. They're already separated. After that it's a matter of bit-shifting and ORing. – user4581301 Jun 08 '20 at 04:55

1 Answers1

-2

unsigned long long int datatype instead of int does the work.

Modified code:

int main() {
 int t;
 cin>>t;
for(int k=1;k<=t;k++)
    {   
        //n=binary no.,nd=decimal equivalent of n
        unsigned long long int n,nd=0,i=0;//IMP
        cin>>n;
        while(n>0)
        {   
            nd+=(n%10)*pow(2,i);
            i++;
            n/=10;
        }
        cout<<nd<<endl;
    }
    return 0;
}
Bhavana
  • 11
  • 1
  • 4
  • Dividing and remaindering by 10 doesn't make any sense along with `pow(2,i)` in a binary-decimal conversion. It should be either divider/remainder by 10 and `pow(10,i)` or divide/remainder by 2 and `pow(2,i)`. This code happens to work but only because it implicitly treats the original binary number as base-10 not as base-2. – user207421 Jun 09 '20 at 04:06