Answer to Question #184273 in C++ for Bilal

Question #184273

Comment this code line by line

include <iostream>
 
#include <string>
 
using namespace std;
 
 
class Bitkaar{
 
private:
 
    int number;
 
public:
 
    Bitkaar( ){  }
 
      Bitkaar(int n){
 
      number = n;
 
 
    }
 
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 = "";
 
{
 
  rem = n % 16; // Get the remainder.
 
  if (rem > 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;
 
  }
 
  n = n/16;
 
 }
 
 if (s == "")
 
   cout << "0";
 
 else
 
   cout << s;
 
    cout<< endl;
 
 
    }
 
   ~Bitkaar(){
 
    }
 
};
 
int main(){
 
    Bitkaar n01(7801);
 
 
    n01.display();
 
 
    cout<< endl;
 
    Bitkaar n02(10);
 
    n02.display();
 
      return 0;
 
 
}
1
Expert's answer
2021-04-27T23:33:02-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