Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- using namespace std;
- string decToBin(int dec) {
- int i;
- string txt="";
- string bin="";
- while(dec!=0){
- if(dec%2==0){
- txt+="0";
- } else{
- txt+="1";
- }
- dec/=2;
- }
- for(i = txt.size()-1; i >= 0; i--){
- bin+=txt.at(i);
- }
- return bin;
- }
- int binToDec(int bin) {
- int dec = 0;
- int power = 0;
- while (bin != 0) {
- dec += (bin % 10) * pow(2, power);
- power++;
- bin /= 10;
- }
- return dec;
- }
- int main() {
- while (true) {
- cout<< endl<< "1: BIN -> DEC";
- cout<< endl<< "2: DEC -> BIN";
- cout<< endl<< "3: EXIT"<< endl;
- cout<< endl<< "What is your choice: ";
- int choice;
- cin>> choice;
- switch (choice) {
- case 1:
- cout<< endl<< "Enter binary number: ";
- int bin;
- cin>> bin;
- cout<< endl<< "Decimal number: "<< binToDec(bin)<< endl;
- break;
- case 2:
- cout<< endl<< "Enter decimal number: ";
- int dec;
- cin>> dec;
- cout<< endl<< "Binary number:"<< decToBin(dec)<< endl;
- break;
- case 3:
- break;
- default:
- cout<< endl<< "Wrong choice! Try again!" << endl;
- }
- if (choice == 3) {
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement