Binary to Decimal in C using function

convert-Binary-to-Decimal-in-C-using-function

Binary to Decimal in C using function Program Description:

Write a Program to convert the Binary to Decimal in C using function. We need to create a function which takes a binary number as a input and returns decimal equivalent of it., The input number should only contain the digits zero’s and one’s.

Example Input and Output:

Input:

Enter a Binary Number: 0011010

Output:

Binary number 11010 is equal to decimal 26

Pre-Requisites:

It is recommended to go through the following articles to better understand the binary to decimal conversion program using functions.

Binary to Decimal in C using function Algorithm:

  1. Start the program by accepting a integer number from the user and store it variable called num
  2. Define a function called binaryToDecimal, The binaryToDecimal function should accept an long integer as input value and convert it into a decimal value. Here are the details of the binaryToDecimal function.
    • The function accepts a long integer and returns an integer.
    • If the user provides invalid input, binaryToDecimal function return -1 value. The input can be considered invalid if it is a negative number or contains any number other than the zeroes and ones.
    • If the number is valid, Then we use the the for loop to convert the binary number to a decimal number.
    • The logic is to get each digit of the binary number num and use the power function to calculate the power of 2 (as it is a binary number) with respect to the digit at the given position.
    • We use the math.h header file pow function to calculate the power. i.e pow(2, i)
    • Once the loop is terminated, The variable  decimal contains the converted number. return the decimal variable.
  3. Call the binaryToDecimal function from the main function. As the decimalToBinary function returns a integer number, store it in variable called result.
  4. Check the resultvariable
    • if result is -1, Then num is invalid number, display the error message.
    • otherwise, The binaryTodecimal function successfully converted the binary to a decimal number. Display the result.
  5. stop

Binary to Decimal in C using function Program:

As we are using the pow function, We need to include the math.h header file.

Here is the prototype details of the binaryToDecimal function.

  • int <strong>binaryToDecimal</strong>(long int num)
  • function_name: binaryToDecimal
  • arguments_list: long int
  • return_type: int
    • onError returns -1
    • onSuccess returns decimal value

Program Output:

To compile the program we need include the -lm compilation option as we are using the math library’s pow function.

$ gcc binary-decimal-function.c  -lm

The above command produces the executable file ( a.out).

Let’s run the program.

Test Case 1: Try Valid Binary Numbers:

As we can see from the above output, Program is properly converting the binary number into the a decimal value.

Binary-to-Decimal-in-C-using-function-program-output

Test Case 2: Negative numbers and invalid numbers test case:

As excepted, The program is displaying the error message ( Invalid Input, Try Again) on Invalid Numbers like negative numbers and numbers with non-binary digits.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Binary to Decimal Conversion using function in C […]

  2. […] Binary to Decimal Conversion using function in C […]

Leave a Reply