The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
Decimal to Binary
Posted by: Robert | November 19, 2010 @ 6:56pm
C++ Code
[
Download
]
#include <iostream> #include <string> using namespace std; // function returns binary string... even with preceding zeros binary number is still correct. string convertDecimalToBinary(int value); int main() { int x = 0; cout << "Enter a decimal number (max 524288): "; cin >> x; cout << endl; cout << "The number in binary is: " << convertDecimalToBinary(x)<< endl; // call function... return 0; } string convertDecimalToBinary(int value) { const int AMOUNT = 20; int one[AMOUNT]; int two[AMOUNT]; // find binary and put it into an array... for(int i = AMOUNT - 1; i >= 0; i--) { one[i] = value % 2; value = value / 2; } string s1; // create and populate string from array... for (int i = 0; i < AMOUNT; i++) { if (one[i] == 1) { s1.append("1"); } else if (one[i] == 0) { s1.append("0"); } } return s1; }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.53 / 8
36 Votes