C++ Basic
C++ Interview Programs
- C++ Interview Programs
- C++ Abundant Number
- C++ Amicable Number
- C++ Armstrong Number
- C++ Average of N Numbers
- C++ Automorphic Number
- C++ Biggest of three numbers
- C++ Binary to Decimal
- C++ Common Divisors
- C++ Composite Number
- C++ Condense a Number
- C++ Cube Number
- C++ Decimal to Binary
- C++ Decimal to Octal
- C++ Disarium Number
- C++ Even Number
- C++ Evil Number
- C++ Factorial of a Number
- C++ Fibonacci Series
- C++ GCD
- C++ Happy Number
- C++ Harshad Number
- C++ LCM
- C++ Leap Year
- C++ Magic Number
- C++ Matrix Addition
- C++ Matrix Division
- C++ Matrix Multiplication
- C++ Matrix Subtraction
- C++ Matrix Transpose
- C++ Maximum Value of an Array
- C++ Minimum Value of an Array
- C++ Multiplication Table
- C++ Natural Number
- C++ Number Combination
- C++ Odd Number
- C++ Palindrome Number
- C++ Pascalβs Triangle
- C++ Perfect Number
- C++ Perfect Square
- C++ Power of 2
- C++ Power of 3
- C++ Pronic Number
- C++ Prime Factor
- C++ Prime Number
- C++ Smith Number
- C++ Strong Number
- C++ Sum of Array
- C++ Sum of Digits
- C++ Swap Two Numbers
- C++ Triangular Number
C++ Program to Converter a Decimal to Binary
Photo Credit to CodeToFun
π Introduction
In the world of programming, converting numbers from one base to another is a common and essential task. One interesting conversion is from decimal to binary. Decimal is the base-10 number system, and binary is the base-2 system.
Converting a decimal number to its binary equivalent involves representing the decimal value using only the digits 0 and 1.
In this tutorial, we'll explore a C++ program that efficiently converts a decimal number to its binary representation.
π Example
Let's dive into the C++ code that achieves this functionality.
#include <iostream>
// Function to convert decimal to binary
void decimalToBinary(int decimalNumber) {
int binaryNumber[32]; // To store binary digits
int i = 0;
// Convert decimal to binary
while (decimalNumber > 0) {
binaryNumber[i] = decimalNumber % 2;
decimalNumber = decimalNumber / 2;
i++;
}
// Print binary number in reverse order
std::cout << "Binary equivalent: ";
for (int j = i - 1; j >= 0; j--) {
std::cout << binaryNumber[j];
}
std::cout << std::endl;
}
// Driver program
int main() {
// Replace this value with your desired decimal number
int decimalNumber = 15;
// Call the function to convert decimal to binary
decimalToBinary(decimalNumber);
return 0;
}
π» Testing the Program
To test the program with a different decimal number, simply replace the value of decimalNumber in the main function.
Binary equivalent: 1111
Compile and run the program to see the binary equivalent.
π§ How the Program Works
- The program defines a function decimalToBinary that takes a decimal number as input and prints its binary equivalent.
- Inside the function, it uses an array (binaryNumber) to store the binary digits.
- It iterates through the process of dividing the decimal number by 2 and storing the remainder until the decimal number becomes 0.
- The binary digits are stored in reverse order in the array.
- Finally, the binary equivalent is printed.
π§ Understanding the Concept of Decimal to Binary
Before delving into the code, let's briefly understand the binary number system. In binary, each digit represents a power of 2, starting from the rightmost digit as 2^0, then 2^1, 2^2, and so on.
Converting a decimal number to binary involves repeatedly dividing the decimal number by 2 and recording the remainders in reverse order.
For example, the decimal number 15 is equivalent to the binary number 1111.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing optimizations for handling larger decimal numbers.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C++ Program to Converter a Decimal to Binary), please comment here. I will help you immediately.