Home / C / C program for binary to decimal conversion

C program for binary to decimal conversion

Here is given c program for binary to decimal conversion. But before let us see what is a Binary number is? A binary number is a number formed using only two digits i.e 1 or 0. A Decimal number is a number with base 10 which is in used very efficiently for manually calculations.Let us understand the conversion process through example.Suppose we have a binary number 1101 which is needed to be converted to decimal than its equivalent decimal number can be calculated as:

Multiplying 2 with last digit of binary number + Multiplying 21 with second last digit of binary number
and so on upto the number is…i.e
1101 is equivalent to = 2 * 1 + 21 * 0 + 22 * 1 + 23 * 1 = 13.
Hence 13 is decimal equivalent of binary number 1101.

ALSO CHECK: java program for binary to decimal conversion.

Program for binary to decimal conversion in c

#include<stdio.h>
#include<conio.h>
void main()
{
int j=1,num,n,d,rem,dec=0;
clrscr(); // clearing the screen
printf("Enter the number in binary\n");
scanf("%d",&num); // Taking input binary number
n=num; // assingning the num value to n
while(n>0)
{
rem=n%10; // Taking the last digit of binary number
d=rem*j; // j is a variable initialized to 1 which is 2^0
dec=dec+d; // dec is a variable used to store the decimal number after the calculation
j=j*2;
n=n/10; // skipping the last digit and again repeating the loop for next iteration.
}
printf("The Decimal Number is\n");
printf("%d",dec); // output the decimal number.
getch(); // getting the char from keyboard to halt the program
}

OUTPUT:

binary to decimal conversion
C program for binary to decimal conversion

Incoming terms: C program to convert binary number to decimal number.

Check Also

Restrict Mouse Pointer Position C Graphics Program

This C Program will restrict the Mouse Pointer location or to a particular position or x,y ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.