decimal to binary conversion

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 1
Reputation: vinay raja is an unknown quantity at this point 
Solved Threads: 0
vinay raja vinay raja is offline Offline
Newbie Poster

decimal to binary conversion

 
0
  #1
Sep 10th, 2006
Here is the solution to converting decimal no. to binary using while loop
this code is in c++
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. unsigned long int y,i=0,j=0,r,k[50];
  7. unsigned long a,x,b[50];
  8. cout<<"\nEnter a decimal no. ";
  9. cin>>a;
  10. y=a;
  11. x=a-y;
  12. while(y>0)
  13. {
  14. r=y%2;
  15. k[i]=r;
  16. i++;
  17. y=y/2;
  18. }
  19. int m=0;
  20. while(m<10)
  21. {
  22. x=x*2;
  23. b[j]=int(x);
  24. j++;
  25. m++;
  26. if(x>1)
  27. x=x-1;
  28. else
  29. if(x==0)
  30. break;
  31. }
  32. cout<<"\nDecimal no. "<<a<<" = ";
  33. if(a>1)
  34. {
  35. for(int e=i-1;e>=0;e--)
  36. cout<<k[e];
  37. cout<<".";
  38. for(int g=0;g<m;g++)
  39. cout<<b[g];
  40. }
  41. else
  42. {
  43. cout<<"0.";
  44. for(int f=0;f<m;f++)
  45. cout<<b[f];
  46. }
  47. cout<<" in binary no. system";
  48. getch();
  49. }
Last edited by Salem; Sep 10th, 2006 at 12:14 pm. Reason: Please learn to use the code tags to make your code presentable
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: decimal to binary conversion

 
1
  #2
Sep 10th, 2006
There's already one in code snippet
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: decimal to binary conversion

 
2
  #3
Sep 10th, 2006
Some points to point out in your code:

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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: decimal to binary conversion

 
1
  #4
Sep 10th, 2006
What S.O.S. said.

And indent more. 3-4 spaces, not 1, is standard.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC