0

Write a function that converts the binary number to the decimal number. For example "10" in binary will be "2" in decimal.

Input: First line contains N(1<=N<=31). That N symbols are inputed(all are 0 or 1) Output: The number in decimal format Input Output 4 9 1001 How to write numbers without space between them? #include <iostream> #include <cmath> using namespace std; int convert(char arr[31],int n) { double decimalNumber = 0, remainder; for (int i = 0; i < n; i++) { while (arr[i] != 0) { remainder = arr[i] % 10; arr[i] /= 10; decimalNumber += remainder * pow(2, i); ++i; } } return decimalNumber; } int main() { int n; cin >> n; char arr[31]; for (int i = 0; i < n; i++) { cin >> arr[i]; } cout << convert(arr,n) ; return 0; }

11th Oct 2020, 8:32 AM
Azat Malgazhdar
Azat Malgazhdar - avatar
1 Réponse