Write a program of Conversion between number system in C language

Exploring Number System Conversion in C: A Comprehensive Guide

Introduction:
In the realm of computer science and programming, understanding different number systems and their conversions is essential. Whether you're working with binary, decimal, octal, or hexadecimal numbers, being able to convert between them programmatically can be incredibly useful. In this blog post, we'll delve into the concept of number system conversions and demonstrate how to implement a program in the C programming language to convert numbers between different bases.

Understanding Number Systems:
A number system is a mathematical notation for representing numbers of a given set, consistently using digits or symbols. The most common number systems encountered in computer science are:
1. Binary (base 2): Uses two symbols, typically 0 and 1.
2. Decimal (base 10): Uses ten symbols, 0 through 9.
3. Octal (base 8): Uses eight symbols, 0 through 7.
4. Hexadecimal (base 16): Uses sixteen symbols, 0 through 9 and A through F.

Conversion between Number Systems:
Converting numbers between different bases involves converting the digits of a number from one base to another while preserving its value. The process typically involves dividing the number by the base repeatedly and recording the remainder or using algorithms specific to each conversion.

Implementing Number System Conversion in C:
Now, let's see how we can implement a program in C to convert numbers between different bases, specifically from decimal to binary, octal, and hexadecimal.

#include <stdio.h>
// Function to convert decimal to binary
void decimalToBinary(int n) {
    int binaryNum[32];
    int i = 0;
    while (n > 0) {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    for (int j = i - 1; j >= 0; j--)
        printf("%d", binaryNum[j]);
}
// Function to convert decimal to octal
void decimalToOctal(int n) {
    int octalNum[100];
    int i = 0;
    while (n != 0) {
        octalNum[i] = n % 8;
        n = n / 8;
        i++;
    }
    for (int j = i - 1; j >= 0; j--)
        printf("%d", octalNum[j]);
}
// Function to convert decimal to hexadecimal
void decimalToHexadecimal(int n) {
    char hexaDeciNum[100];
    int i = 0;
    while (n != 0) {
        int temp = 0;
        temp = n % 16;
        if (temp < 10) {
            hexaDeciNum[i] = temp + 48;
            i++;
        } else {
            hexaDeciNum[i] = temp + 55;
            i++;
        }
        n = n / 16;
    }
    for (int j = i - 1; j >= 0; j--)
        printf("%c", hexaDeciNum[j]);
}
int main() {
    int decimalNumber;
    // Input the decimal number from the user
    printf("Enter a decimal number: ");
    scanf("%d", &decimalNumber);
    // Convert decimal number to binary
    printf("Binary equivalent: ");
    decimalToBinary(decimalNumber);
    printf("\n");
    // Convert decimal number to octal
    printf("Octal equivalent: ");
    decimalToOctal(decimalNumber);
    printf("\n");
    // Convert decimal number to hexadecimal
    printf("Hexadecimal equivalent: ");
    decimalToHexadecimal(decimalNumber);
    printf("\n");
    return 0;
}

Conclusion:
In this blog post, we've explored the concept of number system conversions and demonstrated how to implement a program in the C programming language to convert numbers between different bases, specifically from decimal to binary, octal, and hexadecimal. By understanding the principles behind number system conversions and employing the provided code, you can now perform conversions between different number systems programmatically, enabling you to handle a wide range of numerical tasks effectively. Whether you're a beginner or an experienced programmer, mastering such fundamental operations is crucial for developing robust and versatile software solutions.

Comments