CPP Tutorial

CPP examples programs

CPP program to convert a decimal number into a binary number

Decimal System: A number system with base 10 i.e, numbers from 0-9 are used to represent a decimal number system.

Binary System: A number system with base 2 i.e, only binary numbers 0 and 1 are used to represent a binary number system.

Code:

#include <iostream.h>
using namespace std;
 
int main()
{  
int arr[20];
int num,i;    
 
cout << "Enter the number: ";    
cin >> num;  
int n= num;
 
for(i=0;num>0;i++)    
{    
arr[i] = num%2;    
num = num/2;    
}   
 
cout << "Binary Value of " << n << " is: ";
for(i=i-1; i>=0; i--)    
cout << arr[i];    
return 0;
}

Output

Enter the number: 15
Binary value of 15 is: 1111