| | |
decimal to binary conversion
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 1
Reputation:
Solved Threads: 0
Here is the solution to converting decimal no. to binary using while loop
this code is in c++
this code is in c++
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> void main() { clrscr(); unsigned long int y,i=0,j=0,r,k[50]; unsigned long a,x,b[50]; cout<<"\nEnter a decimal no. "; cin>>a; y=a; x=a-y; while(y>0) { r=y%2; k[i]=r; i++; y=y/2; } int m=0; while(m<10) { x=x*2; b[j]=int(x); j++; m++; if(x>1) x=x-1; else if(x==0) break; } cout<<"\nDecimal no. "<<a<<" = "; if(a>1) { for(int e=i-1;e>=0;e--) cout<<k[e]; cout<<"."; for(int g=0;g<m;g++) cout<<b[g]; } else { cout<<"0."; for(int f=0;f<m;f++) cout<<b[f]; } cout<<" in binary no. system"; getch(); }
Last edited by Salem; Sep 10th, 2006 at 12:14 pm. Reason: Please learn to use the code tags to make your code presentable
There's already one in code snippet
http://www.daniweb.com/code/snippet87.html
Try to learn something from it.
http://www.daniweb.com/code/snippet87.html
Try to learn something from it.
The key to eliminating bugs from your code is learning from your mistakes.
Some points to point out in your code:
1.
2. Dont use
3. Dont use old style headers in C++. Use something like the
4. Dont use
1.
int main (void) is the correct prototype for the main function and not void main.2. Dont use
#include <conio.h> which is console based I/O library, it kills program portability.3. Dont use old style headers in C++. Use something like the
#include <iostream> along with using namespace std ; to use the I/O functionality.4. Dont use
clrscr() to clear the screen since its a non standard function. Last edited by Salem; Sep 10th, 2006 at 1:15 pm. Reason: Fix a tag
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Help With Decimal to Binary conversion (IT Professionals' Lounge)
- Convert decimal to binary and with base the 8 (C)
- Help! Decimal to binary conversion (C)
- decimal > binary > Oct > Hex (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ : Calculating the average value of an array
- Next Thread: Help! error LNK2001 & error LNK
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






