Q1.You are required to write a computer program that accepts a decimal number and generates a corresponding binary, octal, and hexadecimal output.
Q2. Write a program that accepts binary input string and generates corresponding octal and hexadecimal outputs respectively.
Q1.
#include <iostream>
#include <string>
using namespace std;
string Binary(int number)
{
string bin;
while(number!=0) {bin=(number%2==0 ?"0":"1")+bin; number/=2;}
return bin;
}
int main(){
int n;
cout << "Enter the decimal number: ";
cin >> dec >> n;
cout << "The octal value = "
<< oct << n << endl;
cout << " The hexadecimal value = "
<< hex << n << endl;
cout<<"The binary value = "<<Binary(n);
}
Q2
#include<iostream>
using namespace std;
int Decimal(int number)
{
int nu = number;
int dec = 0;
int b = 1;
int tempo = number;
while (tempo) {
int lastDigit = tempo % 10;
tempo = tempo / 10;
dec += lastDigit * b;
b = b* 2;
}
return dec;
}
int main()
{
int n = 10101001;
int number = Decimal(n); //Converting the binary string to decimal
cout << "The octal value = "
<< oct << number << endl;
cout << " The hexadecimal value = "
<< hex << number << endl;
}
Comments
Leave a comment