I am writing a program that will convert a base ten decimal number to a base two
ID: 3822041 • Letter: I
Question
I am writing a program that will convert a base ten decimal number to a base two binary reprentation in scientific notation. The program works for some cases, like 3.75 which prints 1.111E1
but it doen't work for longer test cases such as 140.1 which is 1.0001100000110011001101E7
or for -7916.718605 print -1.11101110110010111E12 can someone help me find the bug in my program?
Thanks.
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
int k =0, integral, i = 1, pos=-9;
long binary = 0;
float fractional, temp, floatNum;
double binaryTotal;
char str[50];
void removeTrailZero(char *a);
printf("Convert float to binary ");
printf(" Enter float value : ");
scanf("%floatNum",&floatNum);
integral = (int)floatNum;
//Separating the fractional part of the value
fractional = floatNum - (int)floatNum;
//convert integral part to Binary
while(integral>0)
{
binary = binary + integral % 2 * i;
if (integral % 2 == 1)
pos = k;
i = i * 10;
k++;//k calculates the position it is calcuation
integral = integral / 2;
}
k = 0; //reset k
//Loop for converting Fractional value to binary
while(k < 8 && fractional!=0)
{
temp = fractional *2;
binary = binary*10 +((int)temp);
k ++;
if ( -k > pos && ((int)temp) == 1)
pos = -k;
fractional = temp - (int)temp;
}
//combine the integral and fractional part.
binaryTotal = (float)binary/pow(10,(pos+k));
sprintf(str,"%lf", binaryTotal); //convert binary to string
removeTrailZero(str); //this function goes thought the string and removes zeros
printf(" binary equivalent = %sE%d ", str,pos);
}
void removeTrailZero(char *a) { //this function goes though the string and keeps on removing zeros
int i, len = strlen(a);
for (i = len-1; i>2; i--)
if (a[i] == '0')
a[i] = '';
else if (a[i] == '1')
return;
}
Explanation / Answer
This answer I have divided into two parts but you can implement it collectively
1. Converting Decimal To Binary
#include <stdio.h>
#include <conio.h>
long dToB(long value);
int main() {
long input_value_decimal;
printf("Enter a input_value_decimal numberalue");
scanf("%ld", &input_value_decimal);
printf("input_value_binary number of %ld is %ld", input_value_decimal, dToB(input_value_decimal));
getch();
return 0;
}
long dToB(long value) {
int rem;
long input_value_binary = 0, incr = 1;
while(value != 0) {
rem = value%2;
value = value/2;
input_value_binary= input_value_binary + (rem*incr);
incr = incr*10;
}
return input_value_binary;
}
2. Convert Binary To Decimal
#include <stdio.h>
#include <conio.h>
#include <math.h>
long bToD(long value);
int main() {
long input_value_binary;
printf("Enter a input_value_binary numberalue");
scanf("%ld", &input_value_binary);
printf("input_value_decimal number of %ld is %ld", input_value_binary, bToD(input_value_binary));
getch();
return 0;
}
long bToD(long value) {
int rem;
long input_value_decimal = 0, a=0;
while(value != 0) {
rem = value%10;
value = value/10;
input_value_decimal = input_value_decimal + (rem*pow(2,a));
++a;
}
return input_value_decimal;
}
Hope it helps..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.