fork download
  1. #include <iostream>
  2. using namespace std;
  3. class BD
  4. {
  5. int p;
  6. public:
  7. void convert(int n)
  8. {
  9. int num = n;
  10. int dec_value = 0;
  11. int base = 1;
  12. int temp = num;
  13. while (temp) {
  14. int last_digit = temp % 10;
  15. temp = temp / 10;
  16. dec_value += last_digit * base;
  17. base = base * 2;
  18. }
  19. cout<<"Decimal To Binary: "<<dec_value;
  20. }
  21.  
  22. void convert()
  23. {
  24. p=25;
  25. int binaryNum[32];
  26. int i = 0;
  27. while (p > 0) {
  28. binaryNum[i] = p % 2;
  29. p = p / 2;
  30. i++;
  31. }
  32. for (int j = i - 1; j >= 0; j--)
  33. {cout <<binaryNum[j]; }
  34. }
  35.  
  36. };
  37. int main()
  38. {
  39. BD ob;
  40. cout<<"Binary To Decimal: ";
  41. ob.convert();
  42. cout<<endl;
  43. ob.convert(10101);
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
Binary To Decimal: 11001
Decimal To Binary: 21