Program for Decimal to Binary Conversion | C ++ program to convert a decimal number to binary number ; function to convert decimal to binary ; array to store binary number ; counter for binary array ; storing remainder in binary array ; printing binary array in reverse order ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void decToBinary ( int n ) { int binaryNum [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } for ( int j = i - 1 ; j >= 0 ; j -- ) cout << binaryNum [ j ] ; } int main ( ) { int n = 17 ; decToBinary ( n ) ; return 0 ; }