C++ Program to convert Decimal to Binary || Decimal to Binary Conversion Algorithm

 C++ Program to convert Decimal to Binary

We can convert any decimal number (base-10 (0 to 9)) into binary number (base-2 (0 or 1)) by C++ program.

Decimal Number:

Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number example 123, 987, 456, 0, 8 etc.

Binary Number:

Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number example 1000001, 101, 101010 etc.

C++ Programs:

C++ Program to convert Decimal to Binary || Decimal to Binary Conversion Algorithm , dev c++, cpp. code,computer language,computer science,


Source Code:

// C++ program to convert a decimal
// number to binary number
#include <iostream>
/*  iostream provides basic
input and output services for C++ programs */    
using namespace std;  
int main()
// Th execution of program start with the main function      
{  
// a[10] array to store binary number
int a[10], num, i;    
cout<<"Enter the number to convert: ";
// cout is used to display the same statement in the output.        
cin>>num;
// cin is used to enter the value by the user.    
for(i=0; num>0; i++)    
{  
 // storing remainder in binary array  
a[i]=num%2;    
num= num/2;  
}    
cout<<"Binary of the given number= ";
// printing binary array in reverse order    
for(i=i-1 ;i>=0 ;i--)    
{    
cout<<a[i];    
}    

Decimal to Binary Conversion Algorithm

  • Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array
  • Step 2: Divide the number by 2 through / (division operator)
  • Step 3: Repeat the step 2 until the number is greater than zero.

Post a Comment

0 Comments