Convert Decimal to Binary in C++

Decimal Number: It ranges from 0 to 9. The base of decimal number is 10. Combination of any numbers is called decimal number. For example, 45, 88, 456, etc.

Binary Number : Any combination of binary numbers 0 and 1 is refers as binary number. The base of the binary number is 0 and 1. Only two numbers are binary 0 and 1.

We can convert decimal number into binary number in C++ language.

Convert Decimal to Binary in C++

Convert Decimal to Binary in C++

#include <iostream>  
using namespace std;  
int main()  
{  
int a[10], n, i;    
cout<<"Enter the number to convert: ";    
cin>>n;    
for(i=0; n>0; i++)    
{    
a[i]=n%2;    
n= n/2;  
}    
cout<<"Binary of the given number= ";    
for(i=i-1 ;i>=0 ;i--)    
{    
cout<<a[i];    
}    
}  

The decimal number is divided by 2 through percentage operator and store the remainder in an array. Divide the number by 2 through division operator. This step repeats until the number is greater than zero.

Read Also : Linked List Program in C++

Output

The screen asks you to enter any decimal number. Here number 9 is entered. The binary number of decimal number 9 is 1001.

Enter the number to convert: 9
Binary of the given number= 1001

Final Words

I hope this article helps you to Convert Decimal to Binary in C++ Program. If you face any issues please let me know via the comment section. Share this article with other C++ program developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment