Created
November 22, 2022 22:34
-
-
Save SPACE-DOGGO/457dcabcf7286105dc7df11f0bdd2342 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int decimalToBinary() | |
{ | |
int num; | |
int place = 1; | |
int res = 0; | |
cout << "Введите число: "; | |
cin >> num; | |
while (num) | |
{ | |
res += (num % 2) * place; | |
num = num / 2; | |
place = place * 10; | |
} | |
return res; | |
} | |
int binaryToDecimal() | |
{ | |
int num; | |
int place = 1; | |
int res = 0; | |
cout << "Введите число: "; | |
cin >> num; | |
int temp = num; | |
while (temp) | |
{ | |
int lastDigit = temp % 10; | |
temp = temp / 10; | |
res += lastDigit * place; | |
place = place * 2; | |
} | |
return res; | |
} | |
int main() | |
{ | |
setlocale(0, "UKR"); | |
int choice; | |
cout << | |
"1. Перевод из десятичной системы в бинарную\n\ | |
2. Перевод из бинарной системы в десятичную\n\n>> "; | |
cin >> choice; | |
cout << "\n"; | |
if (choice == 1) | |
{ | |
cout << decimalToBinary() << "\n"; | |
} | |
else if (choice == 2) | |
{ | |
cout << binaryToDecimal() << "\n"; | |
} | |
else | |
{ | |
system("CLS"); | |
main(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment