Answer to Question #175881 in C++ for zain ul abdeen

Question #175881

Create a class named Bitkaar. Take the number 65535. Create functions to print in binary, decimal, and hexadecimal. Print number in binary by using the algorithm discussed in the class. For Decimal and Hexadecimal build a logic by your own thinking.


1
Expert's answer
2021-03-27T10:34:47-0400
#include <iostream>

#include <string>

 

using namespace std;

 

class Bitkaar{

private:

    int number;

   

public:

    Bitkaar( ){  }

   

      Bitkaar(int n){  

      number = n;

    } 

    //Function to display dec, bin, hex

void display(){

int binN[10], i, n; 

n = number; 

 

cout<<"Decimal: " <<number << endl;

 

for(i=0; n>0; i++)   {   

   binN[i]=n%2;   

   n= n/2; 

}   

cout<<"Binary: ";   

for(i=i-1 ;i>=0 ;i--)  {   

     cout<<binN[i];   

}          

 cout<<endl;

 

 n=number;

 int rem;

 string s = "";

while (n > 0)   // Do this while the quotient is greater than 0.

{

  rem = n % 16; // Get the remainder.

  if (rem > 9)

  {

    // Map the character given that the remainder is greater than 9.

    switch (rem)

    {

      case 10: s = "A" + s; break;

      case 11: s = "B" + s; break;

      case 12: s = "C" + s; break;

      case 13: s = "D" + s; break;

      case 14: s = "E" + s; break;

      case 15: s = "F" + s; break;

    }

  }

  else

  {

    s = char(rem + 48) + s; // Converts integer (0-9) to ASCII code.

    // x + 48 is the ASCII code for x digit (if 0 <= x <= 9)

  }

  n = n/16;

 }

 if (s == "") // if the number was 0, the string will remain empty

   cout << "0";

 else

   cout << s;

  

    cout<< endl;

       

    }

    //Destructor

    ~Bitkaar(){

    }

};

int main(){

 

    Bitkaar n01(7801);

    n01.display();

    cout<< endl;

 

    Bitkaar n02(10);

    n02.display();

   

      return 0;

}

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