This article describes about how to convert number from decimal to binary in C/C++? Let us discuss about the decimal to binary conversion system.
Convert from Decimal to Binary
The decimal (base ten) numeral system has ten possible values (0, 1, 2,3,4,5,6,7,8, or 9). The binary (base two) numeral system has two possible values (0 or 1). Binary system is the internal language of electronic computers. That’s why any computer programmer must need to know how to convert a decimal to binary. Follow C++ code will convert a decimal number into binary number.
C/C++ Code to Convert Decimal to Binary
#include #include void main() { int result[30],i,d,j,a; clrscr(); cout<<"\" This program will convert decimal number into binary number. \""<<endl<<endl; xx: cout<<"What is the number ?. Give a decimal integer number:"<<endl; cin>>d; if(d<0) { cout<<"Negative number is not allow, Please give a non negative integer number. "<<endl; goto xx; } a=d; i=0; while(d>1) { result[i]=d%2; d=d/2; i++; } result[i]=d; cout<<endl; cout<<"Binary number of Decimal "<<a<<" is : "; for(j=i;j>=0;j--) cout<<result[j]; getch(); }
excellent program.