Answer to Question #217440 in C++ for kojo

Question #217440

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.


1
Expert's answer
2021-07-15T05:19:01-0400

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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS