A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C program to convert number from Binary to Decimal
Q:

C program to convert number from Binary to Decimal

0

C program to convert number from Binary to Decimal

In this program we will read Binary Value of a Number and converts it into Decimal Number Susyemr System. This program is for Binary to Decimal Conversion in C.

The logic behind to implement this program - Access each digit from the Binary Number multiply the digit by the power of 2 (for first digits from right side multiply digit with 2^0, second digits 2^1 and so on), add the result and finally you will get Decimal value of given Binary Number. Here we will multiply with the power of base and base of Binary number is 2.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Binary to Decimal Conversion using C program

/*C program to convert number from binary to decimal*/

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    char bin[32] = { 0 };
    int dec, i;
    int cnt; /*for power index*/

    printf("Enter binary value: ");
    gets(bin);

    cnt = 0;
    dec = 0;
    for (i = (strlen(bin) - 1); i >= 0; i--) {
        dec = dec + (bin[i] - 0x30) * pow((double)2, (double)cnt);
        cnt++;
    }

    printf("DECIMAL value is: %d", dec);

    return 0;
}

Output:

    Enter binary value: 1000100001
    DECIMAL value is: 545

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now