Binary to decimal in c

 Problem Statement

Write a c program to convert the binary digits to decimal number.

Solution

#include<stdio.h>

int main()

{

    int n,rem,base=1,decimal=0;

    scanf("%d",&n);

    while(n>0)

    {

        rem=n%10;

        decimal=decimal+rem*base;

        n=n/10;

        base=base*2;

    }

    printf("%d",decimal);

}

Input

1111 

Output                                                                                                                            

15 

Program Explanation

  • In this c program, we are going to see how to convert binary digits into decimal number.

  • Here we declare header file stdio.h for input and output standard function files and integer variables as n, rem, base and decimal.

  •  Using scanf function we are accepting the input binary digits.

  • Then using while loop converting the binary digits into decimal number stored in the decimal variable.

  •  Printing the final result value of this program using printf function.
Binary to Decimal c program


Rajesh

This Blog will help you to find out the c programs solution easily and also you can able to understand the program.Please follow our blog for more useful updates.

Post a Comment (0)
Previous Post Next Post