Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. // HW_5a
  2. // Programmer- Nafis Chowdhury
  3. // Main.cpp file
  4. //=========================================
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. //===========Function Prototype===================
  10. void decToBinary(int num1);
  11.  
  12.  
  13. //==============Main Function====================
  14. int main()
  15. {
  16. int num1;
  17.  
  18. cout << "Enter a non-negative integer value: ";
  19. cin >> num1;
  20.  
  21. if (num1 < 0)
  22. {
  23. cout << endl << "Invalid Entry." << endl << endl;
  24. }
  25. else
  26. {
  27. cout << endl << "Decimal " << num1 << " = ";
  28. decToBinary(num1);
  29. cout << endl << endl;
  30. }
  31.  
  32. return 0;
  33. }
  34.  
  35.  
  36. //==================decToBinary Function====================
  37. void decToBinary(int num1)
  38. {
  39. int remainder;
  40.  
  41. num1 = num1 / 2;
  42. remainder = num1 % 2;
  43.  
  44. if (num1 > 0)
  45. {
  46. decToBinary(num1);
  47.  
  48. }
  49. else if (num1 = 0)
  50. {
  51. cout << "0";
  52. return;
  53. }
  54.  
  55. cout << remainder;
  56. }
  57.  
  58.  
  59. //=================OUTPUT==========================
  60.  
  61. Enter a non-negative integer: 0
  62. Decimal to binary = 0
  63. Press any key to continue....
  64.  
  65. Enter a non-negative integer: 37
  66. Decimal to binary = 0100101
  67. Press any key to continue....
  68.  
  69. Enter a non-negative integer: -1
  70. Decimal to binary = Invald!
  71. Press any key to continue....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement