0

Here is the solution to converting decimal no. to binary using while loop
this code is in c++

#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();
}
Votes + Comments
BAD CODE
4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by WaltP
2

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.

Votes + Comments
You're right (by andor)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.