Convert Binary to Decimal number system with C
/*
Input
Input number: 0011
Output
Decimal: 3
Example:-
11010101 = 1*2^0 + 1*2^1 + 0*2^2 + 1*2^3 + 0*2^4 + 1*2^5 + 0*2^6 + 1*2^7
*/
#include <stdio.h>
#include <math.h>
int main()
{
long long binary, decimal = 0, tempBinary;
int N = 0, BASE = 2;
printf("Enter any binary number: ");
scanf("%lld", &binary);
tempBinary = binary;
while (tempBinary != 0)
{
/* If current binary digit is 1 */
if (tempBinary % 10 == 1)
{
decimal += pow(BASE, N);
}
N++;
tempBinary /= 10;
}
printf("Binary number = %lld\n", binary);
printf("Decimal number= %lld", decimal);
return 0;
}
Output:-
Enter any binary number : 11010101
Binary number = 11010101
Decimal number = 213
0 Comments