Binary to Decimal Conversion

Convert a given binary number, represented as a string 'S' of size 'N', into its decimal equivalent integer and output it.

Input:

The first line contains an integer 'T', denoting the number of test cases.
The first line of each test case contains a single integer 'N', the length of the binary string.
The second line contains the binary string 'S'.

Output:

Print the decimal equivalent for each test case on a new line.

Example:

Input:
2
3
101
4
1101

Output:
5
13

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 30

Note:

A binary string consists solely of '0' and '1' characters.

AnswerBot
12d

Convert a binary string to its decimal equivalent integer.

  • Iterate through the binary string from right to left, multiplying each digit by 2 raised to the power of its position.

  • Add the results of the m...read more

Help your peers!
Add answer anonymously...
Blackrock Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter