C programs to convert decimal to binary have been shown here.
1. C Program to convert binary to decimal
Code has been copied
/************************************* alphabetacoder.com C program to convert binary to decimal **************************************/ #include <stdio.h> #include <math.h> int main() { // declare variables long bin, num = 0; int r, i = 0; // take input printf("Enter the number in binary: "); scanf("%ld", & bin); // convert the number in decimal while (bin > 0) { r = bin % 10; num = num + r * pow(2, i); bin = bin / 10; i++; } // display the number in decimal printf("Decimal: %ld\n", num); return 0; }
Output
Case 1:
Enter the number in binary: 1110
Decimal: 14
Case 2:
Enter the number in binary: 111111
Decimal: 63
2. C Program to convert binary to decimal using recursion
Code has been copied
/**************************** alphabetacoder.com C program to convert binary to decimal using recursion *****************************/ #include <stdio.h> // function to display // binary to decimal int binary_to_decimal(long bin) { if (bin == 0) return 0; else return (bin % 10 + 2 * binary_to_decimal(bin / 10)); } int main() { // declare variables long bin; // take input printf("Enter the number in binary: "); scanf("%ld", & bin); // call function to display // the binary to decimal printf("Decimal: %ld\n", binary_to_decimal(bin)); return 0; }
Output
Case 1:
Enter the number in binary: 1110
Decimal: 14
Case 2:
Enter the number in binary: 111111
Decimal: 63