WAP Binary to Decimal in C

Views: 893
Comments: 0
Like/Unlike: 0
Posted On: 30-Jul-2017 04:47 

Share:   fb twitter linkedin
reena
122 Points
12 Posts

This program will convert binary number (0 and 1) to decimal (0 to 9):

#include<stdio.h>
#include<conio.h>

void main()
{  
    long int binaryNumber, decimalNumber = 0, j = 1, remainder;
    clrscr();
    printf("Enter any number any binary number: ");
    scanf("%ld",&binaryNumber);

    while(binaryNumber != 0)
    {
        remainder = binaryNumber % 10;
        decimalNumber = decimalNumber + remainder * j;
        j = j * 2;
        binaryNumber = binaryNumber / 10;
    }
    printf("Equivalent decimal value: %ld", decimalNumber);
    getch();
}

 Sample output: 

Enter any number any binary number: 1111
Equivalent decimal value: 15

0 Comments
 Log In to Chat