Home › Forums › C Programming › plzz help me out ………..
- This topic has 3 replies, 2 voices, and was last updated 14 years, 9 months ago by DeenaDeBernales.
- AuthorPosts
- February 20, 2010 at 3:46 am #2240DeenaDeBernalesParticipant
I need a source code in c that converts binary fraction to decimal fraction without using array or pointer.
for example if i give input value .1101 i will get .8125 - February 21, 2010 at 10:39 pm #3636GWILouisaxwzklaParticipant
Could do this:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : February 21 , 2010<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 1:00:53 a.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />char ch;<br />int i;<br />char newline = 10;<br />float decimalEquivalent = 0;<br />float multiplier = .5;<br /><br />cout << "enter a binary fraction: " ;<br /><br />ch = cin.get(); //get decimal point<br />ch = cin.get() ; //get first digit of number<br /><br />while ( ch != newline ) //while not at the end of input<br />{<br />decimalEquivalent = decimalEquivalent + ( ch - '0' ) * multiplier;<br />multiplier = multiplier * .5;<br />ch = cin.get() ; // get next digit<br />}<br /><br />cout << endl << endl;<br />cout << "decimal equivalent is : " << decimalEquivalent << endl;<br />}<br /><br /><br /><br /><br /> - February 28, 2010 at 5:44 am #3637DeenaDeBernalesParticipant
#include
#include
//#include
void main()
{
float bin;
float dec=0,multiplier=.5;
int temp,i=1,p; clrscr();
printf(“ENTER ANY BINARY NUMBER: “);
scanf(“%f”,&bin);
while(bin>0.0)
{
temp=(bin*10);printf(“%dn”,temp);
bin=bin*10;
printf(“%fn”,bin);
bin=bin-temp;
printf(“nbin=%fn”,bin);
// p=pow(2,i);dec=(dec+(multiplier*temp));
multiplier=multiplier*.5;
}
printf(“nnnTHE EQUIVALENT DECIMAL NUMBER IS: %f”,dec);
getch();
}I have written this code btt this is not working.. for example suppose if i take .11 then after 2 nd time rotation of while loop the value of bin becomes 0.000000 bt when it goes to check with while loop that is (0.000000>0)it evalutes true ………which is not desired ……..how can i overcome this …………………..
- February 28, 2010 at 5:44 am #3638DeenaDeBernalesParticipant
#include
#include
//#include
void main()
{
float bin;
float dec=0,multiplier=.5;
int temp,i=1,p; clrscr();
printf(“ENTER ANY BINARY NUMBER: “);
scanf(“%f”,&bin);
while(bin>0.0)
{
temp=(bin*10);printf(“%dn”,temp);
bin=bin*10;
printf(“%fn”,bin);
bin=bin-temp;
printf(“nbin=%fn”,bin);
// p=pow(2,i);dec=(dec+(multiplier*temp));
multiplier=multiplier*.5;
}
printf(“nnnTHE EQUIVALENT DECIMAL NUMBER IS: %f”,dec);
getch();
}I have written this code btt this is not working.. for example suppose if i take .11 then after 2 nd time rotation of while loop the value of bin becomes 0.000000 bt when it goes to check with while loop that is (0.000000>0)it evalutes true ………which is not desired ……..how can i overcome this …………………..
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.