Binary to decimal C program
In this post, you will learn how to convert a binary number into an equivalent decimal using the C programming language. Here, we have mentioned three ways to convert a binary to a decimal number.
A binary number consists of two numbers 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language that is 0 or 1, all inputs given to a computer are decoded by it into series of 0's or 1's to process it further. Any combination of 0 and 1 is a binary number such as 1011, 100, 101011, 111011 etc.
A decimal number is a base-10 number system. It ranges from 0 to 9 i.e., 0,1,2,3,4,5,6,7,8,9. Any combination of digits is a decimal number such as 61, 22, 912, 0, 5 etc. The main advantages of the decimal number system are easy-to-read, used by humans and easy to manipulate.
C convert binary to decimal using while loop
In the given C program, we ask the user to enter a number in binary number system to convert that number into decimal number system using the while loop.
#include <stdio.h>
void main()
{
// declaration of variables
int num, binary_num, decimal_num = 0, base = 1, rem;
printf("Enter a binary number: ");
scanf(" %d", &binary_num);
// print the binary number
printf("The binary number: %d \t", binary_num);
// convert binary to decimal
while(binary_num > 0)
{
/* divide the binary number by 10 and store the remainder in rem variable. */
rem = binary_num % 10;
decimal_num = decimal_num + rem * base;
// divide the number with quotient
binary_num = binary_num / 10;
base = base * 2;
}
// print the decimal
printf("\nThe decimal number: %d \t", decimal_num);
}
Output of the above code:
Enter a binary number: 1101
The binary number: 1101
The decimal number: 13
Enter a binary number: 1110
The binary number: 1110
The decimal number: 14
C convert binary to decimal using for loop
In the given C program, we ask the user to enter a number in binary number system to convert that number into decimal number system using the for loop.
#include <stdio.h>
void main()
{
// declaration of variables
int i,num, binary_num, decimal_num = 0, rem;
printf("Enter a binary number: ");
scanf(" %d", &binary_num);
// print the binary number
printf("The binary number: %d \t", binary_num);
// convert binary to decimal
for (i = 0; binary_num != 0; ++i)
{
rem = binary_num % 10;
binary_num = binary_num / 10;
decimal_num = decimal_num + (rem) * ( pow (2, i));
}
// print the decimal
printf("\nThe decimal number: %d \t", decimal_num);
}
Output of the above code:
Enter a binary number: 1111
The binary number: 1111
The decimal number: 15
Enter a binary number: 11011
The binary number: 11011
The decimal number: 27
C convert binary to decimal using function
In the given C program, we create a custom function binaryTodecimal() to convert the user entered binary to decimal number.
#include <stdio.h>
int binaryTodecimal(int bin_num);
int main()
{
// declare the local variable
int binary_num, dec_num;
printf("Enter a binary number: ");
scanf("%d", &binary_num);
// call the binaryTodecimal() function
dec_num = binaryTodecimal (binary_num);
printf("The decimal number: %d", dec_num);
}
// user-defined function
int binaryTodecimal( int bin_num)
{
// declaration of variables
int dec_num = 0, temp = 0, rem;
// convert binary to decimal
while (bin_num != 0)
{
rem = bin_num % 10;
bin_num = bin_num / 10;
dec_num = dec_num + rem * pow( 2, temp);
temp++;
}
return dec_num;
}
Output of the above code:
Enter a binary number: 11011
The decimal number: 27
Enter a binary number: 101011
The decimal number: 43
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative