C Program to Convert Binary to Decimal

C program to convert binary to decimal; Through this tutorial, we will learn how to convert binary number to decimal number in c program using for loop, while loop, function, and recursion.

Algorithm to Convert Binary to Decimal

Use the following 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 * 2i)
    • Step 5.4: Increment i by 1
  • Step 6: Display the decimal number, d
  • Step 7: Stop

Programs to Convert Binary to Decimal in C

  • 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

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 output 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 output 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 output 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 output of the above c program; as follows:

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

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *