Digital Logic Design

1. C Program to Convert Binary to Decimal

				
					#include <stdio.h>

int main() {
    long long binary, decimal = 0; // Declare variables to store binary and decimal numbers
    int remainder;

    printf("Enter a binary number: ");
    scanf("%lld", &binary); // Get binary number from user

    while (binary != 0) {
        remainder = binary % 10; // Get the last digit of the binary number
        decimal = decimal * 2 + remainder; // Update decimal value using the formula: decimal = decimal * 2 + remainder
        binary /= 10; // Remove the last digit from the binary number
    }

    printf("The decimal equivalent is: %lld\n", decimal); // Print the decimal equivalent

    return 0;
}
				
			

2. C Program to Convert Decimal to Binary

				
					#include <stdio.h>

int main() {
    int decimal, binary[32], i = 0; // Declare variables to store decimal number, binary array, and index

    printf("Enter a decimal number: ");
    scanf("%d", &decimal); // Get decimal number from user

    while (decimal != 0) {
        binary[i++] = decimal % 2; // Store the current binary digit (remainder when divided by 2) in the binary array
        decimal /= 2; // Divide the decimal number by 2 to get the next digit
    }

    printf("The binary equivalent is: ");
    for (int j = i - 1; j >= 0; j--) // Iterate over the binary array in reverse order
        printf("%d", binary[j]); // Print each binary digit
    printf("\n");

    return 0;
}
				
			

3. C Program to Convert Decimal to Octal

				
					#include <stdio.h>

int main() {
    int decimal, octal[20], i = 0; // Declare variables to store decimal number, octal array, and index

    printf("Enter a decimal number: ");
    scanf("%d", &decimal); // Get decimal number from user

    while (decimal != 0) {
        octal[i++] = decimal % 8; // Store the current octal digit (remainder when divided by 8) in the octal array
        decimal /= 8; // Divide the decimal number by 8 to get the next digit
    }

    printf("The octal equivalent is: ");
    for (int j = i - 1; j >= 0; j--) // Iterate over the octal array in reverse order
        printf("%d", octal[j]); // Print each octal digit
    printf("\n");

    return 0;
}
				
			

4. C Program to Convert Octal to Decimal

				
					#include <stdio.h>

int main() {
    int octal, decimal = 0, i = 0; // Declare variables to store octal number, decimal equivalent, and position

    printf("Enter an octal number: ");
    scanf("%o", &octal); // Get octal number from user

    while (octal != 0) {
        decimal += (octal % 10) * (1 << (3 * i)); // Convert current octal digit to decimal and add to decimal
        octal /= 10; // Remove the current octal digit
        i++; // Increment position
    }

    printf("The decimal equivalent is: %d\n", decimal); // Print the decimal equivalent

    return 0;
}
				
			

5. C Program to Convert Hexadecimal to Binary

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

// Function to convert a hexadecimal number to its binary equivalent
void hexToBinary(char hex[], char binary[]) {
    int i = 0;

    while (hex[i]) {
        switch (hex[i]) {
            case '0':
                strcat(binary, "0000"); // Append "0000" to binary
                break;
            case '1':
                strcat(binary, "0001"); // Append "0001" to binary
                break;
                // ... (cases for '2' to '9' omitted for brevity)
            case 'A':
            case 'a':
                strcat(binary, "1010"); // Append "1010" to binary
                break;
            case 'B':
            case 'b':
                strcat(binary, "1011"); // Append "1011" to binary
                break;
                // ... (cases for 'C', 'D', 'E', and 'F' omitted for brevity)
        }
        i++;
    }
}

int main() {
    char hex[17], binary[65] = ""; // Declare arrays for hexadecimal and binary

    printf("Enter a hexadecimal number: ");
    scanf("%16s", hex); // Get hexadecimal number from user

    hexToBinary(hex, binary); // Convert hexadecimal to binary

    printf("The binary equivalent is: %s\n", binary); // Print the binary equivalent

    return 0;
}