ccplusplus.com
Learn C, C++ Concepts
Tuesday, February 21, 2012
decimal to binary conversion c++
/************************************************************************* * File : decimal-to-binary.cpp * Author : Saurabh Gupta * Desc : decimal to binary conversion c++ * Source : http://ccplusplus.com/p/c.html * Created : 8:22 PM Tuesday, February 21, 2012 *************************************************************************/ #include
using namespace std; int toBinary (int); int main() { int nNumDec; cout << "\nEnter Any Number in Decimal\t: "; cin >> nNumDec; toBinary(nNumDec); cout << "\n"; return 0; } //function to convert deciaml to binary int toBinary (int n) { int r; r = n%2; n = n/2; if ( n == 0 ) { cout << "\nThe binary equivalent is " << r; return(r); } else { toBinary(n); } cout << r; } /* * OUTPUT * [sgupta@rhel6x86 cpp]$ c++ decimal-to-binary.c -o decimal-to-binary [sgupta@rhel6x86 cpp]$ ./decimal-to-binary Enter Any Number in Decimal : 10 The binary equivalent is 1010 [sgupta@rhel6x86 cpp]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment