This is a C++ Program to Convert a Decimal Number to its Binary Equivalent.
The program takes a decimal number and converts it into its binary equivalent.
1. The program takes a decimal number.
2. Using a while loop, the binary equivalent is calculated.
3. The result is printed,
Here is the source code of C++ Program to Convert a Decimal Number to its Binary Equivalent. The program output is shown below.
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;
1. The user is asked to enter a decimal number and it is stored in the variable ‘num’.
2. Using a while loop, the modulus of num and 2 is stored in the variable bin and printed.
3. num is then equal to num divided by 2.
4. The loop continues till num is greater than 0.
5. The result is then printed.
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
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.