Algorithm


Binary to Decimal Conversion

1. Start
2. Initialize variables: binary_num, decimal_num = 0, base = 1, remainder
3. Read binary_num from the user
4. Repeat the following steps until binary_num > 0: a. Set remainder = binary_num % 10 b. Set decimal_num = decimal_num + remainder * base c. Set binary_num = binary_num / 10 d. Set base = base * 2
5. Display decimal_num as the result
6. End

Decimal to Binary Conversion

1. Start
2. Initialize variables: decimal_num, binary_num = 0, base = 1, remainder
3. Read decimal_num from the user
4. Repeat the following steps until decimal_num > 0: a. Set remainder = decimal_num % 2 b. Set binary_num = binary_num + remainder * base c. Set decimal_num = decimal_num / 2 d. Set base = base * 10
5. Display binary_num as the result
6. End

These algorithms assume that binary_num and decimal_num are variables representing the binary and decimal numbers, respectively. The algorithms use a loop to process each digit of the binary or decimal number and update the result accordingly.

 

Code Examples

#1 Code Example-C Programing to Convert Binary Number to Decimal

Code - C Programming

// convert binary to decimal

#include <stdio.h>
#include <math.h>

// function prototype
int convert(long long);

int main() {

  long long n;

  printf("Enter a binary number: ");
  scanf("%lld", &n);

  printf("%lld in binary = %d in decimal", n, convert(n));

  return 0;
}

// function definition
int convert(long long n) {

  int dec = 0, i = 0, rem;

  while (n != 0) {

    // get remainder of n divided by 10
    rem = n % 10;

    // divide n by 10
    n /= 10;

    // multiply rem by (2 ^ i)
    // add the product to dec
    dec += rem * pow(2, i);

    // increment i
    ++i;
  }

  return dec;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a binary number: 1101
1101 in binary = 13 in decimal

#2 Code Example-C Program to convert decimal number to binary

Code - C Programming

// convert decimal to binary

#include <stdio.h>
#include <math.h>

// function prototype
long long convert(int);

int main() {
    
  int n;
  long long bin;
  
  printf("Enter a decimal number: ");
  scanf("%d", &n);
  
  // convert to binary using the convert() function
  bin = convert(n);
  
  printf("%d in decimal =  %lld in binary", n, bin);

  return 0;
}

// function to convert decimal to binary
long long convert(int n) {

  // variable to store the result
  long long bin = 0;

  int rem, i = 1;

  // loop to convert to binary
  while (n != 0) {
    
    // get remainder of n divided by 2
    rem = n % 2;
    
    // divide n by 2
    n /= 2;
    
    // multiply remainder by i
    // add the product to bin
    bin += rem * i;
    
    // multiply i by 10
    i *= 10;
  }

  return bin;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a binary number: 1101
1101 in binary = 13 in decimal
Advertisements

Demonstration


C Programing Example to Convert Binary Number to Decimal and vice-versa-DevsEnv

Next
Appending into a File in C Programming