Number Base Conversion in C – Decimal, Binary and Octal

Number Base Conversion in C

This is a Number Base Conversion Program written in C. It is designed to assist novice C programmers in understanding and implementing basic number base conversions of numeral system. This program allows users to perform the following conversions:

  1. Decimal to Binary: Converts a decimal number to its binary equivalent.
  2. Decimal to Octal: Converts a decimal number to its octal equivalent.
  3. Octal to Decimal: Converts an octal number to its decimal equivalent.
  4. Octal to Binary: Converts an octal number to its binary equivalent.
  5. Binary to Decimal: Converts a binary number to its decimal equivalent.
  6. Binary to Octal: Converts a binary number to its octal equivalent.

New C programmers can use this program as a reference to grasp fundamental concepts of number base conversions and modular programming in the C language.

This program utilizes several features of the C programming language to achieve its functionality. Here are some of the key C features used in the program. It also uses functions from the math.h library for mathematical operations.

  1. Functions: The program uses functions to encapsulate specific conversion tasks. Functions such as binaryToDecimal, decimalToBinary, octalToDecimal, decimalToOctal, octalToBinary, and binaryToOctal are defined to perform individual conversion operations. This modular approach enhances code readability and maintainability.
  2. Switch Statement: The program employs a switch statement to provide a menu-driven interface. The user can select the desired conversion option by entering a corresponding menu number. The switch statement directs the program flow based on the user’s choice.
  3. Loops (while, for): The program utilizes while loops in the decimalToBinary, decimalToOctal, octalToBinary, and binaryToOctal functions for iterative processes. Additionally, a for loop is used in the binaryToDecimal and octalToDecimal functions to iterate through the input string.
  4. Arrays (Strings): The program makes use of character arrays (strings) to store binary, octal, and decimal representations. Arrays like input, binaryOutput, octalOutput, binaryOutputOctal, and octalOutputBinary are employed to store user input and conversion results.
  5. Standard Input/Output: The program relies on standard input/output functions (printf and scanf) for user interaction. It prompts users for input and displays conversion results through the console.
  6. Math Library (pow function): The program uses the pow function from the math library to perform power calculations. This is employed in the binaryToDecimal and octalToDecimal functions to calculate the decimal equivalent of binary and octal numbers.
  7. Conditional Statements: The program includes conditional statements (if) to check for specific conditions, such as determining whether a binary digit is ‘1’ in the binaryToDecimal function.

Number Base Conversion Program in C Language

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

// Function prototypes
int binaryToDecimal(char* binary);
void decimalToBinary(int decimal, char* binary);
int octalToDecimal(char* octal);
void decimalToOctal(int decimal, char* octal);
void octalToBinary(char* octal, char* binary);
void binaryToOctal(char* binary, char* octal);

int main() {
    int choice;
    char input[50];

    printf("Number Base Conversion Program\n");
    printf("1. Binary to Decimal\n");
    printf("2. Decimal to Binary\n");
    printf("3. Octal to Decimal\n");
    printf("4. Decimal to Octal\n");
    printf("5. Octal to Binary\n");
    printf("6. Binary to Octal\n");

    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
    case 1:
        printf("Enter binary number: ");
        scanf("%s", input);
        printf("Decimal equivalent: %d\n", binaryToDecimal(input));
        break;
    case 2:
        printf("Enter decimal number: ");
        int decimalInput;
        scanf("%d", &decimalInput);

        char binaryOutput[50];
        decimalToBinary(decimalInput, binaryOutput);

        printf("Binary equivalent: %s\n", binaryOutput);
        break;
    case 3:
        printf("Enter octal number: ");
        scanf("%s", input);
        printf("Decimal equivalent: %d\n", octalToDecimal(input));
        break;
    case 4:
        printf("Enter decimal number: ");
        int decimalInputOctal;
        scanf("%d", &decimalInputOctal);

        char octalOutput[50];
        decimalToOctal(decimalInputOctal, octalOutput);

        printf("Octal equivalent: %s\n", octalOutput);
        break;
    case 5:
        printf("Enter octal number: ");
        scanf("%s", input);

        char binaryOutputOctal[50];
        octalToBinary(input, binaryOutputOctal);

        printf("Binary equivalent: %s\n", binaryOutputOctal);
        break;
    case 6:
        printf("Enter binary number: ");
        scanf("%s", input);

        char octalOutputBinary[50];
        binaryToOctal(input, octalOutputBinary);

        printf("Octal equivalent: %s\n", octalOutputBinary);
        break;
    default:
        printf("Invalid choice\n");
    }

    return 0;
}

// Function to convert binary to decimal
int binaryToDecimal(char* binary) {
    int decimal = 0;
    int length = strlen(binary);

    for (int i = length - 1, j = 0; i >= 0; i--, j++) {
        if (binary[i] == '1') {
            decimal += pow(2, j);
        }
    }

    return decimal;
}

// Function to convert decimal to binary
void decimalToBinary(int decimal, char* binary) {
    int index = 0;

    while (decimal > 0) {
        binary[index++] = (decimal % 2) + '0';
        decimal /= 2;
    }

    binary[index] = '\0';

    // Reverse the binary string
    int start = 0;
    int end = index - 1;

    while (start < end) {
        // Swap characters at start and end
        char temp = binary[start];
        binary[start] = binary[end];
        binary[end] = temp;

        // Move towards the center
        start++;
        end--;
    }
}

// Function to convert octal to decimal
int octalToDecimal(char* octal) {
    int decimal = 0;
    int length = strlen(octal);

    for (int i = length - 1, j = 0; i >= 0; i--, j++) {
        decimal += (octal[i] - '0') * pow(8, j);
    }

    return decimal;
}

// Function to convert decimal to octal
void decimalToOctal(int decimal, char* octal) {
    int index = 0;

    while (decimal > 0) {
        octal[index++] = (decimal % 8) + '0';
        decimal /= 8;
    }

    octal[index] = '\0';

    // Reverse the octal string
    int start = 0;
    int end = index - 1;

    while (start < end) {
        // Swap characters at start and end
        char temp = octal[start];
        octal[start] = octal[end];
        octal[end] = temp;

        // Move towards the center
        start++;
        end--;
    }
}

// Function to convert octal to binary
void octalToBinary(char* octal, char* binary) {
    int decimal = octalToDecimal(octal);
    decimalToBinary(decimal, binary);
}

// Function to convert binary to octal
void binaryToOctal(char* binary, char* octal) {
    int decimal = binaryToDecimal(binary);
    decimalToOctal(decimal, octal);
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post