In this tutorial, i am going to show you how to convert binary number to decimal number in c program with the help of for loop, while loop, function, and recursion.

## Algorithm and Programs to Convert Binary to Decimal in C

- Algorithm to Convert Binary to Decimal
- C Program to Convert Binary to Decimal using While Loop
- C Program to Convert Binary to Decimal using For Loop
- C Program to Convert Binary to Decimal using Function
- C Program to Convert Binary to Decimal using Recursion

### Algorithm to Convert Binary to Decimal

Just follow the below given algorithm to write a program to convert binary number to decimal number; as follows:

- Step 1: Start
- Step 2: Read the binary number from the user, say ‘n’
- Step 3: Initialize the decimal number, d=0
- Step 4: Initialize i=0
- Step 5: Repeat while n != 0:
- Step 5.1: Extract the last digit by: remainder = n % 10
- Step 5.2: n = n/10
- Step 5.3: d = d + (remainder * 2
^{i}) - Step 5.4: Increment i by 1

- Step 6: Display the decimal number, d
- Step 7: Stop

### C Program to Convert Binary to Decimal using While Loop

#include <stdio.h> int main() { int binary, decimal = 0, base = 1, remainder; printf("Enter the Binary Number = "); scanf("%d", &binary); int temp = binary; while(temp > 0) { remainder = temp % 10; decimal = decimal + remainder * base; temp = temp / 10; base = base * 2; } printf("The Binary Value = %d\n", binary); printf("The Decimal Value = %d\n", decimal); return 0; }

The result of the above c program; as follows:

Enter the Binary Number = 1101 The Binary Value = 1101 The Decimal Value = 13

### C Program to Convert Binary to Decimal using For Loop

#include <stdio.h> int main() { int binary, decimal = 0, base = 1, remainder, temp; printf("Enter the Binary Number = "); scanf("%d", &binary); for(temp = binary; temp > 0; temp = temp / 10) { remainder = temp % 10; decimal = decimal + remainder * base; base = base * 2; } printf("The Binary Value = %d\n", binary); printf("The Decimal Value = %d\n", decimal); return 0; }

The result of the above c program; as follows:

Enter the Binary Number = 110101 The Binary Value = 110101 The Decimal Value = 53

### C Program to Convert Binary to Decimal using Function

#include <stdio.h> #include <math.h> int binaryToDecimal(int binary) { int decimal = 0, i = 0, remainder; while(binary != 0) { remainder = binary % 10; decimal = decimal + (remainder * pow(2, i)); binary = binary / 10; ++i; } return decimal; } int main() { int binary, decimal; printf("Enter the Binary Number = "); scanf("%d", &binary); decimal = binaryToDecimal(binary); printf("The Binary Value = %d\n", binary); printf("The Decimal Value = %d\n", decimal); return 0; }

The result of the above c program; as follows:

Enter the Binary Number = 010101 The Binary Value = 10101 The Decimal Value = 21

### C Program to Convert Binary to Decimal using Recursion

#include<stdio.h> int BinaryToDecimal(int n) { if(n==0) return 0; else return (n% 10 + 2* BinaryToDecimal(n / 10)); } int main() { int n; printf("Enter the Binary Value:"); scanf("%d",&n); printf("Decimal Value of Binary number is: %d",BinaryToDecimal(n)); }

The result of the above c program; as follows:

Enter the Binary Value:1101 Decimal Value of Binary number is: 13