Binary to Decimal Converter in C

Binary to Decimal Converter in C

This program converts binary numbers to decimal numbers. Below is the C program along with an explanation of its structure and functionality.

Program Structure

The program is structured as follows:

  • Include necessary libraries: The program includes the standard input-output library #include <stdio.h>.
  • Function prototypes: Prototypes for the functions used in the program are declared.
  • Main function: The main function handles user input and output, calling the appropriate functions to convert the binary number to a decimal number.
  • Conversion function: A function that performs the actual conversion from binary to decimal.

C Program


#include <stdio.h>

// Function prototype for binary to decimal conversion
int binaryToDecimal(int binary);

int main() {
    int binaryNumber;

    // Prompt the user to enter a binary number
    printf("Enter a binary number: ");
    scanf("%d", &binaryNumber);

    // Convert binary to decimal
    int decimalNumber = binaryToDecimal(binaryNumber);

    // Display the result
    printf("The decimal equivalent of %d is %d\n", binaryNumber, decimalNumber);

    return 0;
}

/**
 * Function to convert a binary number to a decimal number
 * @param binary The binary number to be converted
 * @return The decimal equivalent of the binary number
 */
int binaryToDecimal(int binary) {
    int decimal = 0, base = 1, remainder;

    while (binary > 0) {
        remainder = binary % 10;
        decimal = decimal + remainder * base;
        binary = binary / 10;
        base = base * 2;
    }

    return decimal;
}
    

Explanation

Libraries: The program includes the standard input-output library #include <stdio.h> to enable input and output operations.

Function prototype: The prototype int binaryToDecimal(int binary); declares the function that will perform the conversion from binary to decimal.

Main function:

  • The program starts by declaring an integer variable binaryNumber.
  • It then prompts the user to enter a binary number using printf and reads the input using scanf.
  • The binary number entered by the user is passed to the binaryToDecimal function, which returns the decimal equivalent.
  • Finally, the result is printed to the screen using printf.

Conversion function:

  • The function binaryToDecimal takes an integer binary as an argument and initializes three variables: decimal (to store the result), base (to keep track of the power of 2), and remainder (to store the remainder when the binary number is divided by 10).
  • The function uses a while loop to iterate through each digit of the binary number. Inside the loop:
    • The remainder when the binary number is divided by 10 is calculated and stored in remainder.
    • This remainder is then multiplied by the current base (power of 2) and added to decimal.
    • The binary number is divided by 10 to remove the last digit, and base is multiplied by 2 to move to the next power of 2.
  • Once the loop completes, the function returns the decimal equivalent.


By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)