0% found this document useful (0 votes)
33 views7 pages

Task No.#1:-: Aimen Mughal 01-133192-014 BEE-3A OOP Open Ended Lab

The document describes a C++ program that uses classes to convert between decimal, binary, and hexadecimal number systems. The class contains functions to convert decimal to binary, decimal to hexadecimal, binary to decimal, and binary to hexadecimal. The main function gets user input for the number and conversion type and calls the appropriate class function. The program allows converting numbers up to 1024 in decimal and 10 bits in binary.

Uploaded by

aimen
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)
33 views7 pages

Task No.#1:-: Aimen Mughal 01-133192-014 BEE-3A OOP Open Ended Lab

The document describes a C++ program that uses classes to convert between decimal, binary, and hexadecimal number systems. The class contains functions to convert decimal to binary, decimal to hexadecimal, binary to decimal, and binary to hexadecimal. The main function gets user input for the number and conversion type and calls the appropriate class function. The program allows converting numbers up to 1024 in decimal and 10 bits in binary.

Uploaded by

aimen
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/ 7

Aimen Mughal

01-133192-014
BEE-3A
OOP Open Ended Lab
Task no.#1:-
You are supposed to design a number system converter by using classes in c++.

User can enter the number in decimal or binary format. A menu will ask the user
to convert,

 Decimal to binary,
 Decimal to hexadecimal
 Binary to decimal
 Binary to Hexadecimal.
Each conversion is to be done in a separate function of class. Binary input number
can be of maximum 10 bits. Decimal number input can be maximum of 1024.

Program:-
#include <iostream>
#include <math.h>
using namespace std;

class Conversion {
public:

void decToBin(int n) {

if (n > 1024) {
cout << " Cant convert more than 1024";
}
else
for (int i = 10; i >= 0; i--) {
int j = n >> i;
if (j & 1)
cout << "1";
else
cout << "0";
}
}

void decTohexa(int n)
{
if (n > 1024) {
cout << " Cant convert more than 1024";
}

else {

char hexaNum[10];

int i = 0;
while (n != 0)
{

int temp = 0;
temp = n % 16;

if (temp < 10)


{
hexaNum[i] = temp + 48;
i++;
}
else
{
hexaNum[i] = temp + 55;
i++;
}
n = n / 16;
}

for (int j = i - 1; j >= 0; j--)


cout << hexaNum[j];
}
}

void binToDec(int n) {

int count = 0;
int num1 = n;
while (num1 != 0)
{
num1 = num1 / 10;
count++;
}
if (count > 10) {
cout << " Can't Convert!! More than 10 bits";
}

else {
int num2 = n;
int decvalue = 0;

int base = 1;

int temp = num2;


while (temp) {
int last = temp % 10;
temp = temp / 10;

decvalue += last * base;

base = base * 2;
}
cout << decvalue << endl;
}
}

void binTohexa(int n) {
int i, temp = 0, hexa[20], rem, num2;

int num1 = n;
int count = 0;
while (num1 != 0)
{
num1 = num1 / 10;
count++;
}
if (count > 10) {
cout << " Cant Convert !! More than 10 bits";
}

else {
num2 = n;
for (i = 0; num2 > 0; i++)
{
for (int j = 0; j < 4; j++)
{
rem = num2 % 10;
num2 /= 10;
temp += rem * pow(2, j);
}
hexa[i] = temp;
temp = 0;
}
while (i > 0)
{
i--;
if (hexa[i] > 9)
cout << char(55 + hexa[i]);
else
cout << hexa[i];
}
}
}
};

int main()
{
Conversion Obj;
int number;
cout << "Enter number for conversion: ";
cin >> number;
cout << "Menu:" << endl;
cout << "1. Decimal to binary" << endl;
cout << "2. Decimal to hexadecimal" << endl;
cout << "3. Binary to decimal" << endl;
cout << "4. Binary to hexadecimal" << endl;
cout << "Enter Choice (1-4):" << endl;
int option;
cin >> option;
switch (option) {
case 1: Obj.decToBin(number);
break;
case 2: Obj.decTohexa(number);
break;
case 3: Obj.binToDec(number);
break;
case 4: Obj.binTohexa(number);
break;
default: cout << "Invalid Choice" << endl;
}
return 0;
}
Outputs:-
Conclusion:-
In this lab we have learnt how to design number system
converter by using classes in which we have done to
convert decimal to binary, decimal to hexadecimal Binary
to decimal Binary to Hexadecimal in this lab by using
separate function class.

You might also like