Skip to content

Instantly share code, notes, and snippets.

@SPACE-DOGGO
Created November 22, 2022 22:34
Show Gist options
  • Save SPACE-DOGGO/457dcabcf7286105dc7df11f0bdd2342 to your computer and use it in GitHub Desktop.
Save SPACE-DOGGO/457dcabcf7286105dc7df11f0bdd2342 to your computer and use it in GitHub Desktop.
#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