Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views10 pages

INHAM11

The document contains a C++ assignment with multiple questions focused on converting decimal numbers to binary and hexadecimal formats using various programming techniques. It includes code snippets for converting decimal to binary using if/else statements, converting decimal to hexadecimal without built-in functions, and handling binary to decimal conversions with detailed steps. Each question is accompanied by a corresponding C++ program that demonstrates the required functionality.

Uploaded by

pgjarrar
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)
8 views10 pages

INHAM11

The document contains a C++ assignment with multiple questions focused on converting decimal numbers to binary and hexadecimal formats using various programming techniques. It includes code snippets for converting decimal to binary using if/else statements, converting decimal to hexadecimal without built-in functions, and handling binary to decimal conversions with detailed steps. Each question is accompanied by a corresponding C++ program that demonstrates the required functionality.

Uploaded by

pgjarrar
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/ 10

NAME : Inhamullah khan

Reg No:SP25-BSE-076

CLASS : BSE

SUBJECT : ICT

TEACHER : SIR AFTAB

ASSIGNMENT 2

QUESTION 1 :
Write a C++ program that converts a decimal number into its binary equivalent by using only if/else
statements

ANSWER
#include<iostream>
using namespace std;
int main()
{
char a, a1, a2, a3;
int decimal b, b1, b2, b3;

cout<<"enter four bit decimal number";


cin>>a, a1, a2, a3;
if(a == '1')
{b = 8;}
else
{b = 0}

if(a1 == '1')
{b1 = 4;}
else
{b1 = 0}

if(a2 == '1')
{b2 = 2;}
else
{b2= 0}

if(a3 == '1')
{b3 = 1;}
else
{b3 = 0}
decimal = b + b1 + b2 + b3;

cout<<" decimal equivalent is : "<< decimal;


return 0;
}

QUESTION 2 :
Write a C++ program to input a decimal number (between 0 and 255) and convert it into
a two-digit hexadecimal number using if/else (without using built-in conversion
functions).

ANSWER:
int main() {
int decimal;
cout << "Enter a decimal number (0-255): ";
cin >> decimal;

if (decimal < 0 || decimal > 255) {


cout << "Please enter a number between 0 and 255." << endl;
return 1;
}

// We will extract each bit from the most significant bit to the least significant bit
int b7, b6, b5, b4, b3, b2, b1, b0;

if (decimal >= 128) {


b7 = 1;
decimal -= 128;
} else {
b7 = 0;
}

if (decimal >= 64) {


b6 = 1;
decimal -= 64;
} else {
b6 = 0;
}

if (decimal >= 32) {


b5 = 1;
decimal -= 32;
} else {
b5 = 0;
}

if (decimal >= 16) {


b4 = 1;
decimal -= 16;
} else {
b4 = 0;
}

if (decimal >= 8) {
b3 = 1;
decimal -= 8;
} else {
b3 = 0;
}

if (decimal >= 4) {
b2 = 1;
decimal -= 4;
} else {
b2 = 0;
}

if (decimal >= 2) {
b1 = 1;
decimal -= 2;
} else {
b1 = 0;
}

if (decimal >= 1) {
b0 = 1;
decimal -= 1;
} else {
b0 = 0;
}

cout << "Binary equivalent: "


<< b7 << b6 << b5 << b4 << b3 << b2 << b1 << b0 << endl;

return 0;
}

QUESTION 4 :
Write a C++ program that inputs a decimal number and breaks it into two hexadecimal digits
using division and modulus operations. Display the final hexadecimal result.

ANSWER:
#include <iostream>
using namespace std;
int main() {
int decimal;
cout << "Enter a decimal number (0-255): ";
cin >> decimal;

if (decimal < 0 || decimal > 255) {


cout << "Please enter a number between 0 and 255." << endl;
return 1;
}

// Split into two hexadecimal digits


int high = decimal / 16; // Most significant hex digit
int low = decimal % 16; // Least significant hex digit

// Function to convert 0–15 into hex character


char hex1, hex2;

if (high < 10)


hex1 = '0' + high;
else
hex1 = 'A' + (high - 10);

if (low < 10)


hex2 = '0' + low;
else
hex2 = 'A' + (low - 10);
// Display result
cout << "Hexadecimal equivalent: " << hex1 << hex2 << endl;

return 0;
}

QUESTION 3 :
In C++, write a program that converts any number between 0 and 255 to its hexadecimal
representation without using arrays or conditional statements.

ANSWER:
#include <iostream>
using namespace std;

int main() {
int decimal;
cout << "Enter a number (0–255): ";
cin >> decimal;

// Ensure number is within 0–255


decimal = (decimal < 0) ? 0 : (decimal > 255 ? 255 : decimal);

int high = decimal / 16;


int low = decimal % 16;

// Use ternary expressions and character math


char hex1 = (high < 10) ? ('0' + high) : ('A' + high - 10);
char hex2 = (low < 10) ? ('0' + low) : ('A' + low - 10);

cout << "Hexadecimal: " << hex1 << hex2 << endl;
return 0;
}

QUESTION 5:
Modify a given C++ program to handle hexadecimal conversion for any 8-bit binary number
and display each step of the conversion process clearly.

ANSWER:
#include <iostream>
#include <string>
using namespace std;

int binaryToDecimal(const string& binary) {


int decimal = 0;
cout << "Binary to Decimal Conversion Steps:\n";
for (int i = 0; i < 8; ++i) {
int bit = binary[i] - '0';
int power = 7 - i;
int value = bit * (1 << power);
cout << "bit[" << i << "] = " << bit << " => " << bit << " * 2^" << power << " = " <<
value << endl;
decimal += value;
}
cout << "Total Decimal Value: " << decimal << endl;
return decimal;
}

char toHexDigit(int value) {


return (value < 10) ? ('0' + value) : ('A' + (value - 10));
}
void decimalToHex(int decimal) {
int high = decimal / 16;
int low = decimal % 16;

cout << "\nDecimal to Hexadecimal Conversion Steps:\n";


cout << "High nibble = " << decimal << " / 16 = " << high << endl;
cout << "Low nibble = " << decimal << " % 16 = " << low << endl;

char hex1 = toHexDigit(high);


char hex2 = toHexDigit(low);

cout << "Hex digits: " << high << " -> '" << hex1 << "', " << low << " -> '" << hex2 << "'"
<< endl;
cout << "\nFinal Hexadecimal: " << hex1 << hex2 << endl;
}

int main() {
string binary;
cout << "Enter an 8-bit binary number: ";
cin >> binary;

// Basic validation
if (binary.length() != 8) {
cout << "Error: Please enter exactly 8 binary digits." << endl;
return 1;
}
for (char c : binary) {
if (c != '0' && c != '1') {
cout << "Error: Binary number must contain only 0 or 1." << endl;
return 1;
}
}

int decimal = binaryToDecimal(binary);


decimalToHex(decimal);

return 0;
}

You might also like