Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HW_5a
- // Programmer- Nafis Chowdhury
- // Main.cpp file
- //=========================================
- #include <iostream>
- using namespace std;
- //===========Function Prototype===================
- void decToBinary(int num1);
- //==============Main Function====================
- int main()
- {
- int num1;
- cout << "Enter a non-negative integer value: ";
- cin >> num1;
- if (num1 < 0)
- {
- cout << endl << "Invalid Entry." << endl << endl;
- }
- else
- {
- cout << endl << "Decimal " << num1 << " = ";
- decToBinary(num1);
- cout << endl << endl;
- }
- return 0;
- }
- //==================decToBinary Function====================
- void decToBinary(int num1)
- {
- int remainder;
- num1 = num1 / 2;
- remainder = num1 % 2;
- if (num1 > 0)
- {
- decToBinary(num1);
- }
- else if (num1 = 0)
- {
- cout << "0";
- return;
- }
- cout << remainder;
- }
- //=================OUTPUT==========================
- Enter a non-negative integer: 0
- Decimal to binary = 0
- Press any key to continue....
- Enter a non-negative integer: 37
- Decimal to binary = 0100101
- Press any key to continue....
- Enter a non-negative integer: -1
- Decimal to binary = Invald!
- Press any key to continue....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement