Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int getDecimalni(int);
- bool isBin(int);
- int main() {
- int broj;
- cout << "Unesite broj u binarnom zapisu: ";
- cin >> broj;
- if (isBin(broj))
- cout << "Decimalni ekvivalent unesenog broja je " << getDecimalni(broj) << ".";
- else
- cout << "Niste upisali binarni broj!";
- system("pause>0");
- return 0;
- }
- int getDecimalni(int broj) {
- int dec = 0, brojac = 0;
- while (broj)
- {
- dec += broj % 10 * pow(2.0, brojac++);
- broj /= 10;
- }
- return dec;
- }
- bool isBin(int broj) {
- while (broj)
- {
- if (broj % 10 != 0 && broj % 10 != 1)
- return false;
- broj /= 10;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement