Binary to Decimal conversion in C: We can convert any binary number(base-2 (0 or 1)) into decimal number (base-10 (0 to 9)) by c program.
Decimal Number
Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.
Binary Number
Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.
To understand this example, you should have the knowledge of the following C programming topics:
C Functions
C User-defined functions
/*Program to convert binary to decimal*/ #include <math.h> #include <stdio.h> int convert(long long n); int main() { long long n; printf("Enter a binary number: "); scanf("%lld", &n); printf("%lld in binary = %d in decimal", n, convert(n)); return 0; } int convert(long long n) { int dec = 0, i = 0, rem; while (n != 0) { rem = n % 10; n /= 10; dec += rem * pow(2, i); ++i; } return dec; }
Output
Enter a binary number: 1001
1001 in binary = 9 in decimal
Enter a binary number: 110011
110011 in binary = 51 in decimal
Programming Solutions @ rajeshshukla
Tutorials | Technical Questions | Interview Questions |
---|---|---|
C Programming C++ Programming Class 11 (Python) Class 12 (Python) |
C Language C++ Programming Python |
C Interview Questions C++ Interview Questions |