Advertisement
angryatti

Binary Number To Decimal - CLI -Cplusplus

Nov 10th, 2023
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. //#include <string.h>
  3. using namespace std;
  4.  
  5. int BinToDec(int binNumber) {
  6.  
  7.     int decimalValue = 0;
  8.     int base1 = 1;
  9.  
  10.     while (binNumber > 0)
  11.     {
  12.         int reminder = binNumber % 10;
  13.         binNumber = binNumber / 10;
  14.         decimalValue += reminder * base1;
  15.         base1 = base1 * 2;
  16.     }
  17.     return decimalValue;
  18. }
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24.  
  25.     char strExit = 'C';
  26.     int bin = 0;
  27.     do
  28.     {
  29.         cout << "Enter Binary Number\n";
  30.         cin >> bin;
  31.  
  32.         cout << BinToDec(bin) << "\n";
  33.  
  34.         cout << "Press Q to exit, C to continue\n";
  35.         cin >> strExit;
  36.         if (strExit == 'Q') {
  37.             break;
  38.         }
  39.     } while (true);
  40.  
  41.     return 0;
  42.  
  43.  
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement