hi i need help i want convert decimal to binary c++ program i made it. But I do not want to use an integer i want convert example 5.2 , 61,3 , 22,4 , 33,50 , 1,7 , 99,33 ... any one help me ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
int num,arr[64];
cout<<"Enter a number : ";
cin>>num;
int i=0,r;
while(num!=0)
{
r = num%2;
arr[i++] = r;
num /= 2;
}
cout<<"Binary :";
for(int j=i-1;j>=0;j--)
cout<<arr[j];
}
If I understand you correctly you want to convert an octal number of 5.2 to a binary number. I will not say that it is impossible or can not be done, but I have not heard of this before.
You might have to break up a floating point number into a left and right set of numbers, convert, then put back together. Just a guess for now.
On one hand I have a better understanding of what you want decimal to binary. Your program does that. On the other hand I do not see any advantage of changing "num" to something other than "int" other than a "short" would limit the range of what "num" can use.
Many people have done this before. You should find something useful with a search here on "decimal to binary".
No offense, but I think that English is not your first language and that is causing a problem with me understanding what you want.
Hello Andy
You re right English not my first language and program convert decimal to binary there s no problem but i want convert decimal (not integer ) like 3,4 66,7 12,2 ... to binary i googleing about decimal to bnary but every example work on integer i dnt want integer i want convert Comma-separated number i hope wii understand
i hope you understand me :) Thank you everything
#include <iostream>
#include <cmath>
usingnamespace std;
char point = '.';
string decToBase( double x, int base, int fracDigits )
{
string result;
bool neg = ( x < 0 ); if ( neg ) x = -x;
// Allow for potential rounding to given precision
x += pow( base, -fracDigits ) / 2.0;
// Split into whole number and fractional parts
int whole = x;
double fraction = x - whole;
// Whole-number part
if ( whole == 0 ) result = "0";
while ( whole > 0 )
{
result = (char)( whole % base + '0' ) + result;
whole /= base;
}
result += point;
// Fractional part
double value = 1.0;
for ( int i = 1; i <= fracDigits; i++ )
{
value /= base;
int num = fraction / value;
result += (char)( num + '0' );
fraction -= num * value;
}
// Deal with sign
if ( neg ) result = '-' + result;
return result;
}
int main()
{
int prec = 8; // digits after point
int base = 2; // number base
double x;
cout << "Input double: ";
cin >> x;
cout << x << " in base " << base << " is " << decToBase( x, base, prec );
}
The language and country differences has caught me off guard. I am not that familiar with people using "3,4" for what I am use to as "3.4", but I know it is used. "3,4" and "3.4" can be refereed to as a decimal number, but better to call it a floating point number if there is any doubt especially if you use a comma.
If you want to use a floating point double then lastchance has a good solution.
I do not remember what I used for a search, either "c++ decimal to binary", "c++ double to binary" or maybe "c++ floating point to binary". Something there should produce some results that you could use.