Shuva_Dev

Binary to Decimal

Nov 17th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.28 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int toDecimal(string binary) {
  5.     int decimal = 0;
  6.  
  7.     for(int i=0; i<binary.size(); i++) {
  8.         decimal *= 2;
  9.         decimal += binary[i] - '0';
  10.     }
  11.     return decimal;
  12. }
  13.  
  14. int main() {
  15.  
  16.     cout << toDecimal("1011");
  17.     return 0;
  18. }
  19.  
Add Comment
Please, Sign In to add comment