Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- void Input(int &number)
- {
- do
- {
- cout << "\nEnter number: ";
- cin >> number;
- if (number < 0)
- {
- cout << "Please enter again" << endl;
- }
- } while (number < 0);
- }
- void DecimalToBinary(int number)
- {
- int binary[32];
- int i = 0;
- while (number > 0)
- {
- binary[i] = number % 2;
- number /= 2;
- i++;
- }
- cout << "Decimal to Binary: ";
- for (int j = i - 1; j >= 0; j--)
- {
- cout << binary[j];
- }
- cout << endl;
- }
- void DecimalToHexadecimal(int number)
- {
- char hexadecimal[100];
- int i = 0, surplus;
- while (number > 0)
- {
- surplus = number % 16;
- if (surplus < 10)
- {
- surplus += 48;
- }
- else
- {
- surplus += 55;
- }
- hexadecimal[i] = surplus;
- number /= 16;
- i++;
- }
- cout << "Decimal to Hexadecimal: ";
- for (int j = i - 1; j >= 0; j--)
- {
- cout << hexadecimal[j];
- }
- cout << endl;
- }
- int BinaryToDecimal()
- {
- char Bin[20] = { 0 };
- long int Dec = 0;
- int i = 0, n;
- cout << "\nEnter Binary number :";
- cin >> Bin;
- while (Bin[i] != '\0')
- {
- n = Bin[i] - 48;
- if ((n != 0) && (n != 1))
- return -1;
- Dec = (Dec * 2) + n;
- i++;
- }
- return Dec;
- }
- void BinaryToHexadecimal(int number)
- {
- unsigned int hexa[32] = { 0 };
- int i = 1, j, rem, dec = 0;;
- //binary to decimal
- while (number > 0)
- {
- rem = number % 2;
- dec = dec + (rem * i);
- number /= 10;
- i *= 2;
- }
- //decimal to hexa
- i = 0;
- while (dec != 0)
- {
- hexa[i] = dec % 16;
- dec /= 16;
- i++;
- }
- cout << "Hexa Decimal valua: ";
- for (j = i - 1; j >= 0; j--)
- {
- if (hexa[j] > 9)
- cout << char(hexa[j] + 55);
- else
- cout << hexa[j];
- }
- cout << endl;
- system("pause");
- return;
- }
- void HexadecimalToDecimal()
- {
- char Hex[17];
- long int Decimal = 0;
- int i = 0, val, len;
- cout << "Enter any Hexadecimal number: ";
- cin >> Hex;
- len = strlen(Hex);
- len--;
- for (i = 0; Hex[i] != '\0'; i++)
- {
- switch (Hex[i])
- {
- case 0:
- val = 0;
- break;
- case 1:
- val = 1;
- break;
- case 2:
- val = 2; break;
- case 3:
- val = 3; break;
- case 4:
- val = 4; break;
- case 5:
- val = 5; break;
- case 6:
- val = 6; break;
- case 7:
- val = 7; break;
- case 8:
- val = 8; break;
- case 9:
- val = 9; break;
- case 'a':
- case'A':
- val = 10; break;
- case 'b':
- case'B':
- val = 11; break;
- case'C':
- case'c':
- val = 12; break;
- case'd':
- case'D':
- val = 13; break;
- case 'E':
- case'e':
- val = 14; break;
- case 'F':
- case'f':
- val = 15; break;
- default:
- break;
- }
- Decimal += val * (pow(16, len));
- len--;
- }
- cout << "Hexadecimal to Decimal : " << Decimal << endl;
- }
- int main()
- {
- int number;
- int selection;
- do
- {
- cout << "1: Decimal to Binary " << endl;
- cout << "2: Decimal to Hexadecimal " << endl;
- cout << "3: Binary to Decimal " << endl;
- cout << "4: Hexadecimal to Decimal " << endl;
- cout << "5: Binary to Hexadecimal " << endl;
- cout << "6: Close(0)" << endl;
- cout << "\nChoose :";
- cin >> selection;
- switch (selection)
- {
- case 1:
- Input(number);
- DecimalToBinary(number);
- break;
- case 2:
- Input(number);
- DecimalToHexadecimal(number);
- break;
- case 3:
- cout << "Binary to Deciaml : " << BinaryToDecimal() << endl;
- break;
- case 4:
- HexadecimalToDecimal();
- break;
- case 5:
- Input(number);
- BinaryToHexadecimal(number);
- break;
- default:
- break;
- }
- } while (selection > 0);
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement