Home Forums C Programming plzz help me out ………..

Viewing 3 reply threads
  • Author
    Posts
    • #2240
      DeenaDeBernales
      Participant

      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

    • #3636
      GWILouisaxwzkla
      Participant

      Could do this:


      /****************************************************************
      * File Name : c:programstempCG.cpp
      * Date : February 21 , 2010
      * Comments : new project
      * Compiler/Assembler : Visual C++ 6.0
      * Modifications :
      *
      *
      *
      *
      *
      * Program Shell Generated At: 1:00:53 a.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


      #include < iostream >
      //#include < string.h >
      //#include < conio.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >

      using namespace std;

      //main function ******************************

      int main ( )
      {

      char ch;
      int i;
      char newline = 10;
      float decimalEquivalent = 0;
      float multiplier = .5;

      cout << "enter a binary fraction: " ;

      ch = cin.get(); //get decimal point
      ch = cin.get() ; //get first digit of number

      while ( ch != newline ) //while not at the end of input
      {
      decimalEquivalent = decimalEquivalent + ( ch - '0' ) * multiplier;
      multiplier = multiplier * .5;
      ch = cin.get() ; // get next digit
      }

      cout << endl << endl;
      cout << "decimal equivalent is : " << decimalEquivalent << endl;
      }




    • #3637
      DeenaDeBernales
      Participant

      #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 …………………..

    • #3638
      DeenaDeBernales
      Participant

      #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 …………………..

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.