top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C program to convert Binary to decimal with floating point?

+2 votes
584 views
C program to convert Binary to decimal with floating point?
posted Mar 7, 2019 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void main()
{
    int a[20],b[20];
    int i,j,k,cntint,cntfra,flag,rem;
    float rem1;
    char s[20];
    cntint=cntfra=flag=rem=0;
    rem1=0.0;
    clrscr();
    printf("Enter the binary no : ");
    scanf("%s",s);
    for(i=0,j=0,k=0;i<strlen(s);i++)
    {
        if(s[i]=='.')
        {
            flag=1;
        }
    elseif(flag==0)
        a[j++]=s[i]-48;
    elseif(flag==1)
        b[k++]=s[i]-48;
    }
    cntint=j;
    cntfra=k;
    for(j=0,i=cntint-1;j<cntint;j++,i--)
    {
        rem = rem + (a[j] * pow(2,i));
    }
    for(k=0,i=1;k<cntfra;k++,i++)
    {
        rem1 = rem1 + (b[k] / pow(2,i));
    }
    rem1 = rem + rem1;
    printf("\nThe decimal value of the given binary is : %f",rem1);
    getch();
}


//@ifour technolab pvt. ltd.
answer Mar 14, 2019 by Rushabh Verma R.
Similar Questions
–1 vote

Determine the set of inputs that will lead to SIGFPE (floating point exception) being triggered in following C program?

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    if(argc != 3 || !atoi(argv[2]))
        return 1;
    return abs(atoi(argv[1])) / atoi(argv[2]);
}
+3 votes

Decimal to Factorial
23(D) = 3210(F)

23 / 3! = 3 (5)
5 / 2! = 2 (1)
1 / 1! = 1 (0)
0 / 0! = 0 (0)

Factorial to Decimal
3210(F)=23(D),
3*3!+2*2!+1*1!+0*0! = 23(D)

...