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();
}``````

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.

commented: You're right (by andor) +2

What S.O.S. said.

And indent more. 3-4 spaces, not 1, is standard.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.