This is a C++ Program to Convert a Decimal Number to its Binary Equivalent.
Problem Description
The program takes a decimal number and converts it into its binary equivalent.
Problem Solution
1. The program takes a decimal number.
2. Using a while loop, the binary equivalent is calculated.
3. The result is printed,
4. Exit.
C++ Program/Source code
Here is the source code of C++ Program to Convert a Decimal Number to its Binary Equivalent. The program output is shown below.
#include
using namespace std;
int main ()
{
int num, bin;
cout << "Enter the number : ";
cin >> num;
cout << "The binary equivalent of " << num << " is ";
while (num > 0)
{
bin = num % 2;
cout << bin;
num /= 2;
}
return 0;
}
Runtime Test Cases
Case 1 :
Enter the number : 3
The binary equivalent of 3 is 11
Case 2 :
Enter the number : 121
The binary equivalent of 121 is 1001111
Case 3 :
Enter the number : 15
The binary equivalent of 15 is 1111