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();
}
Grunt commented: BAD CODE +0

Dani AI

Generated

The solution from demonstrates the usual divide-and-multiply approach (integer part by repeated division, fractional part by repeated multiplication). That works, but earlier replies from and rightly flagged portability and style issues, and pointed to an existing snippet. The following gives a compact, modern and portable alternative that handles integer and fractional parts, shows how to mark non-terminating fractions, and avoids nonstandard headers.

A minimal, idiomatic C++ implementation (standard headers only) — integer part by bit extraction, fractional part by repeated multiply-by-2 with a bounded iteration count to avoid infinite loops for repeating fractions:

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>

std::string intToBinary(unsigned long long v) {
    if (v == 0) return "0";
    std::string s;
    while (v) { s.push_back(char('0' + (v & 1))); v >>= 1; }
    std::reverse(s.begin(), s.end());
    return s;
}

std::string fracToBinary(double frac, int maxBits = 32) {
    std::string s;
    int bits = 0;
    while (frac > 0.0 && bits < maxBits) {
        frac *= 2.0;
        if (frac >= 1.0) { s.push_back('1'); frac -= 1.0; }
        else s.push_back('0');
        ++bits;
    }
    if (frac > 0.0) s += "..."; // indicates non-terminating binary fraction
    return s;
}

Notes and cautions: prefer standard headers (<iostream>, <string>, etc.) and a standard main signature for portability. Negative values are best represented with a leading '-' and converting abs(value) (this avoids conflating numeric conversion with machine two's-complement bit-patterns). Floating-point fractions can be repeating in binary (e.g., 0.1 decimal) — limit fractional bits (20–64) and append an ellipsis to indicate truncation. For fixed-width integer representations, use std::bitset<N> to show a specific width. Finally, pick types that match expected ranges (unsigned long long for large integers) to avoid silent overflow.

Recommended Answers

All 3 Replies

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.