Advertisement
adnanj

Pretvaranje iz binarnog u decimalni brojni sistem

Dec 6th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int getDecimalni(int);
  5. bool isBin(int);
  6.  
  7. int main() {
  8.     int broj;
  9.  
  10.     cout << "Unesite broj u binarnom zapisu: ";
  11.     cin >> broj;
  12.  
  13.     if (isBin(broj))
  14.         cout << "Decimalni ekvivalent unesenog broja je " << getDecimalni(broj) << ".";
  15.     else
  16.         cout << "Niste upisali binarni broj!";
  17.  
  18.     system("pause>0");
  19.     return 0;
  20. }
  21.  
  22. int getDecimalni(int broj) {
  23.     int dec = 0, brojac = 0;
  24.     while (broj)
  25.     {
  26.         dec += broj % 10 * pow(2.0, brojac++);
  27.         broj /= 10;
  28.     }
  29.     return dec;
  30. }
  31.  
  32. bool isBin(int broj) {
  33.     while (broj)
  34.     {
  35.         if (broj % 10 != 0 && broj % 10 != 1)
  36.             return false;
  37.         broj /= 10;
  38.     }
  39.     return true;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement