
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program To Convert Decimal Number to Binary
In a computer system, the binary number is expressed in the binary numeral system while the decimal number is in the decimal numeral system. The binary number is in base 2 while the decimal number is in base 10. Examples of decimal numbers and their corresponding binary numbers are as follows −
Decimal Number | Binary Number |
---|---|
15 | 01111 |
10 | 01010 |
18 | 10010 |
27 | 11011 |
A program that converts the decimal numbers into binary is as follows −
Example
#include <iostream> using namespace std; void DecimalToBinary(int n) { int binaryNumber[100], num=n; int i = 0; while (n > 0) { binaryNumber[i] = n % 2; n = n / 2; i++; } cout<<"Binary form of "<<num<<" is "; for (int j = i - 1; j >= 0; j--) cout << binaryNumber[j]; cout<<endl; } int main() { DecimalToBinary(15); DecimalToBinary(10); DecimalToBinary(18); DecimalToBinary(27); return 0; }
Output
Binary form of 15 is 1111 Binary form of 10 is 1010 Binary form of 18 is 10010 Binary form of 27 is 11011
In the above program, the DecimalToBinary function has binary value of the decimal number n and is stored in the array binaryNumber[]. A while loop is used and the result of the n modulus 2 operation is stored in binaryNumber[] for each iteration of the loop.
This is shown using the following code snippet.
while (n > 0) { binaryNumber[i] = n % 2; n = n / 2; i++; }
After this, the binary number is displayed using a for loop. This is shown as follows −
cout<<"Binary form of "<<num<<" is "; for (int j = i - 1; j >= 0; j--) cout << binaryNumber[j];
The main() function contains only the function calls to DecimalToBinary() for various decimal numbers.
This is shown in the following code snippet.
DecimalToBinary(15); DecimalToBinary(10); DecimalToBinary(18); DecimalToBinary(27);
- Related Articles
- Java Program to convert binary number to decimal number
- Python program to convert decimal to binary number
- Convert decimal to binary number in Python program
- Java program to convert decimal number to binary value
- Java program to convert binary number to decimal value
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- Haskell program to convert a decimal number into a binary number
- C++ Program to Convert Binary Number to Decimal and vice-versa
- Swift program to convert the decimal number to binary using recursion
- C# Program to Convert Binary to Decimal
- Swift Program to convert Decimal to Binary
- Swift Program to convert Binary to Decimal
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- Java Program to convert from decimal to binary
