0% found this document useful (0 votes)
17 views17 pages

DLD Assignment No 2

This document discusses different number conversion methods between decimal, binary, octal and hexadecimal number systems. It includes functions to convert between these number systems and a menu driven program to call these conversion functions.

Uploaded by

masoomfatima1213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views17 pages

DLD Assignment No 2

This document discusses different number conversion methods between decimal, binary, octal and hexadecimal number systems. It includes functions to convert between these number systems and a menu driven program to call these conversion functions.

Uploaded by

masoomfatima1213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Digital logic design

ASSIGNMENT NO 2

SUBMITTED BY :
MUHAMMAD ISMAIL
SUBMITTED TO :
SIR ABRAR
DEPARTMENT :
BS-CS-2 (B)
ROLL NO
23011519-060
DECIMAL TO BINARY :

DECIMAL TO OCTAL:
DECIMAL TO HEXADECIMAL:
Binary to decimal:
Octal to decimal:
Hexadecimal to decimal:
Binary to octal:
Octal to binary
Binary to hexadecimal:
Hexadecimal to binary:
Octal to Hexadecimal:
Hexadecimal to Octal:
Code:
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void intro() {
cout <<
"==========================INTRODUCTION=================================
=" << endl;
cout << " Name : Asad ali rehmat " << endl;
cout << " Roll No. : 23011519-058 " << endl;
cout << " Department : BSCS 2 (A) " << endl;
cout <<
"========================================================================"
<< endl;
}
void display() {
cout << "\n\n-------------- Enter Your Choice -----------------\n";
cout << "1. Decimal to Binary \n";
cout << "2. Decimal to Octal \n";
cout << "3. Decimal to Hexadecimal \n";
cout << "4. Binary to Decimal \n";
cout << "5. Octal to Decimal \n";
cout << "6. Hexadecimal to Decimal \n";
cout << "7. Binary to Octal\n";
cout << "8. Octal to Binary\n";
cout << "9. Binary to Hexadecimal\n";
cout << "10. Hexadecimal to Binary\n";
cout << "11. Octal to Hexadecimal\n";
cout << "12. Hexadecimal to Octal\n";
cout << "13. Exit the program.\n";
cout << "--------------------------------------------------\n\n";
cout << "Enter your choice : \n";
}
string decimalToBinary(int decimal) {
if (decimal == 0) {
return "0";
}
string binary = "";
while (decimal > 0) {
int remainder = decimal % 2;
binary = to_string(remainder) + binary;
decimal = decimal / 2;
}
return binary;
}
string decimalToOctal(int decimal) {
if (decimal == 0) {
return "0";
}
string octal = "";
while (decimal > 0) {
int remainder = decimal % 8;
octal = to_string(remainder) + octal;
decimal = decimal / 8;
}
return octal;
}
string decimalToHexadecimal(int decimal) {
if (decimal == 0) {
return "0";
}
string hexadecimal = "";
char digit;
while (decimal > 0) {
int remainder = decimal % 16;
if (remainder < 10) {
digit = remainder + '0';
}
else {
digit = remainder - 10 + 'A';
}
hexadecimal = digit + hexadecimal;
decimal = decimal / 16;
}
return hexadecimal;
}
int binaryToDecimal(string binary) {
int decimal = 0;
int base = 1;
for (int i = binary.length() - 1; i >= 0; --i) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}
return decimal;
}
int octalToDecimal(string octal) {
int decimal = 0;
int base = 1;
for (int i = octal.length() - 1; i >= 0; --i) {
decimal += (octal[i] - '0') * base;
base *= 8;
}
return decimal;
}
int hexadecimalToDecimal(string hexadecimal) {
int decimal = 0;
int base = 1;
for (int i = hexadecimal.length() - 1; i >= 0; --i) {
if (hexadecimal[i] >= '0' && hexadecimal[i] <= '9') {
decimal += (hexadecimal[i] - '0') * base;
}
else if (hexadecimal[i] >= 'A' && hexadecimal[i] <= 'F') {
decimal += (hexadecimal[i] - 'A' + 10) * base;
}
base *= 16;
}
return decimal;
}
string binaryToOctal(string binary) {
int decimal = binaryToDecimal(binary);
return decimalToOctal(decimal);
}
string octalToBinary(string octal) {
int decimal = octalToDecimal(octal);
return decimalToBinary(decimal);
}
string binaryToHexadecimal(string binary) {
int decimal = binaryToDecimal(binary);
return decimalToHexadecimal(decimal);
}
string hexadecimalToBinary(string hexadecimal) {
int decimal = hexadecimalToDecimal(hexadecimal);
return decimalToBinary(decimal);
}
string octalToHexadecimal(string octal) {
int decimal = octalToDecimal(octal);
return decimalToHexadecimal(decimal);
}
string hexadecimalToOctal(string hexadecimal) {
int decimal = hexadecimalToDecimal(hexadecimal);
return decimalToOctal(decimal);
}
int main() {
int choice;
string number;
intro();
do {
display();
cin >> choice;
if (choice >= 1 && choice <= 12) {
cout << "Enter the number : ";
cin >> number;
}
switch (choice) {
case 1:
cout << "Binary: " << decimalToBinary(stoi(number)) << endl;
break;
case 2:
cout << "Octal: " << decimalToOctal(stoi(number)) << endl;
break;
case 3:
cout << "Hexadecimal: " << decimalToHexadecimal(stoi(number))
<< endl;
break;
case 4:
cout << "Decimal: " << binaryToDecimal(number) << endl;
break;
case 5:
cout << "Decimal: " << octalToDecimal(number) << endl;
break;
case 6:
cout << "Decimal: " << hexadecimalToDecimal(number) << endl;
break;
case 7:
cout << "Octal: " << binaryToOctal(number) << endl;
break;
case 8:
cout << "Binary: " << octalToBinary(number) << endl;
break;
case 9:
cout << "Hexadecimal: " << binaryToHexadecimal(number) << endl;
break;
case 10:
cout << "Binary: " << hexadecimalToBinary(number) << endl;
break;
case 11:
cout << "Hexadecimal: " << octalToHexadecimal(number) << endl;
break;
case 12:
cout << "Octal: " << hexadecimalToOctal(number) << endl;
break;
case 13:
cout << "--------------- Exiting the program. ---------------";
break;
default:
cout << "Your input is invalid.";
}
} while (choice != 13);
return 0;
}

The end (❁´◡`❁)

You might also like