Co Practicals
Co Practicals
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Function to convert Hexadecimal to Decimal
int hexToDec(char hex[]) {
}
return dec_val;
}
// Function to convert Octal to Decimal
int octalToDec(char octal[]) {
int len = strlen(octal);
return dec_val;
}
// Function to convert Binary to Decimal
int binaryToDec(char binary[]) {
int len = strlen(binary);
int base = 1; // 2^0
int dec_val = 0;
for (int i = len - 1; i >= 0; i--) {
if (binary[i] == '1') {
dec_val += base;
}
base = base * 2;
}
return dec_val;
}
// Function to convert Decimal to Hexadecimal
void decToHex(int decimal) {
char hex[100];
int i = 0;
while (decimal != 0) {
int temp = decimal % 16;
if (temp < 10) {
hex[i] = temp + 48;
} else {
hex[i] = temp + 55;
}
i++;
decimal /= 16;
}
// Reverse the array for correct order
for (int j = i - 1; j >= 0; j--) {
printf("%c", hex[j]);
}
}
// Function to convert Decimal to Octal
void decToOctal(int decimal) {
char octal[100];
int i = 0;
while (decimal != 0) {
printf("%c", octal[j]);
}
}
// Function to convert Decimal to Binary
void decToBinary(int decimal) {
char binary[100];
int i = 0;
while (decimal != 0) {
binary[i] = (decimal % 2) + '0';
i++;
decimal /= 2;
}
// Reverse the array for correct order
for (int j = i - 1; j >= 0; j--) {
printf("%c", binary[j]);
}
}
// Driver function to test the conversions
int main() {
int choice;
char input[100];
int decimal;
do {
printf("\nNumber Conversion Menu:\n");
printf("1. Hexadecimal to Decimal\n");
printf("2. Octal to Decimal\n");
printf("3. Binary to Decimal\n");
scanf("%s", input);
printf("Decimal: %d\n", hexToDec(input));
break;
case 2:
printf("Enter an Octal number: ");
scanf("%s", input);
break;
case 4:
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("Hexadecimal: ");
decToHex(decimal);
printf("\n");
break;
case 5:
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("Octal: ");
decToOctal(decimal);
printf("\n");
break;
case 6:
printf("Enter a Decimal number: ");
scanf("%d", &decimal);
printf("Binary: ");
decToBinary(decimal);
printf("\n");
break;
case 0:
printf("Exiting...\n");
break;
default:
OUTPUT
Number Conversion Menu:
1. Hexadecimal to Decimal
2. Octal to Decimal
3. Binary to Decimal
4. Decimal to Hexadecimal
5. Decimal to Octal
6. Decimal to Binary
0. Exit
Enter your choice: 1
decimal += base;
}
base = base * 2;
}
return decimal;
}
// Driver function
int main() {
char bin1[100], bin2[100], result[100];
int choice;
do {
switch (choice) {
case 1:
printf("Enter the first binary number: ");
scanf("%s", bin1);
printf("Enter the second binary number: ");
scanf("%s", bin2);
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 0);
return 0;
}
OUTPUT
Binary Addition & Subtraction Menu:
#include <string.h>
// Function to convert a binary string to decimal integer
int binaryToDecimal(char binary[]) {
int decimal = 0;
int base = 1; // 2^0
int len = strlen(binary);
return decimal;
}
// Function to convert a decimal integer to a binary string
void decimalToBinary(int decimal, char binary[]) {
int index = 0;
// Handle the case of decimal = 0
if (decimal == 0) {
strcpy(binary, "0");
return;
}
while (decimal > 0) {
binary[index++] = (decimal % 2) + '0';
decimal /= 2;
}
binary[index] = '\0';
}
// Function to perform binary multiplication
void binaryMultiplication(char bin1[], char bin2[], char result[]) {
int dec1 = binaryToDecimal(bin1);
int dec2 = binaryToDecimal(bin2);
int product = dec1 * dec2;
OUTPUT
Enter the first binary number: 1010
Enter the second binary number: 1101
Binary Multiplication Result: 11011010
}
return a;
}
// Function to perform binary subtraction using 2's complement
int binarySubtraction(int a, int b) {
// Take 2's complement of b and add it to a
return binaryAddition(a, binaryAddition(~b, 1));
}
int main() {
int a, b, choice;
printf("Enter two numbers (in decimal form):\n");
scanf("%d %d", &a, &b);
printf("Select operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
scanf("%d", &choice);
if (choice == 1) {
printf("Result of Binary Addition: %d\n", binaryAddition(a, b));
} else if (choice == 2) {
printf("Result of Binary Subtraction: %d\n", binarySubtraction(a, b));
} else {
printf("Invalid choice!\n");
}
return 0;
}
return a ^ b;
}
int NOT(int a) {
return ~a & 1; // Ensure it's a single bit
}
// Half adder implementation using logic gates
carry = carryOut;
}
printf("Carry Out: %d\n", carry);
}
// Function to perform 2's complement for subtraction
void twosComplement(int B[], int n) {
}
}
// Function to perform binary subtraction A - B using 2's complement method
void binarySubtraction(int A[], int B[], int result[], int n) {
// Get the 2's complement of B
twosComplement(B, n);
}
void printBinary(int bin[], int n) {
for (int i = 0; i < n; i++) {
printf("%d", bin[i]);
}
printf("\n");
}
int main() {
int n = 4; // Define number of bits for simplicity (4-bit binary numbers)
int A[4], B[4], result[4];
scanf("%d", &B[i]);
}
int choice;
printf("Select operation:\n1. Addition\n2. Subtraction\n");
scanf("%d", &choice);
if (choice == 1) {
// Perform binary addition
printf("Performing Binary Addition (A + B):\n");
binaryAddition(A, B, result, n);
printf("Result (A + B) = ");
printBinary(result, n);
} else if (choice == 2) {
// Perform binary subtraction
printf("Performing Binary Subtraction (A - B):\n");
binarySubtraction(A, B, result, n);
printf("Result (A - B) = ");
printBinary(result, n);
} else {
printf("Invalid choice!\n");
}
return 0;
}
OUTPUT
Enter first 4-bit binary number (A): 1 0 1 0
Enter second 4-bit binary number (B): 0 1 1 0
Select operation:
1. Addition
2. Subtraction
1
Performing Binary Addition (A + B):
Carry Out: 0
Result (A + B) = 11000
Select operation:
1. Addition
2. Subtraction
2
Performing Binary Subtraction (A - B):
Carry Out: 1
Result (A - B) = 0100
6.Implement a c program to perform a logic and shift micro-operations using logic gates.
#include <stdio.h>
}
// Function to print a 4-bit binary number
void printBinary(int bin[], int n) {
for (int i = 0; i < n; i++) {
printf("%d", bin[i]);
}
printf("\n");
}
int main() {
int n = 4; // Number of bits for binary numbers
int A[4], B[4], result[4];
// Input two 4-bit binary numbers
switch (choice) {
case 1:
logicAND(A, B, result, n);
printf("Result (A AND B): ");
printBinary(result, n);
break;
case 2:
logicOR(A, B, result, n);
printf("Result (A OR B): ");
printBinary(result, n);
break;
case 3:
case 5:
shiftLeft(A, result, n);
printf("Result (Logical Shift Left A): ");
printBinary(result, n);
break;
case 6:
printBinary(result, n);
break;
case 9:
rotateRight(A, result, n);
printf("Result (Rotate Right A): ");
printBinary(result, n);
break;
default:
printf("Invalid choice!\n");
break;
}
return 0;
}
OUTPUT
Enter first 4-bit binary number (A): 1 0 1 0
#include <math.h>
// Function to convert decimal to binary (signed 8-bit representation)
void decimalToBinary(int num, int bin[], int n) {
int i;
for (i = 0; i < n; i++) {
bin[i] = num % 2;
num = num / 2;
}
}
// Function to convert binary to decimal (signed 8-bit representation)
int binaryToDecimal(int bin[], int n) {
int decimal = 0;
}
}
// Function to perform binary subtraction (A = A - M)
void binarySubtraction(int A[], int M[], int n) {
int temp[n];
// Compute 2's complement of M
for (int i = 0; i < n; i++) {
temp[i] = (M[i] == 0) ? 1 : 0; // Invert the bits
}
binaryAddition(A, M, n); // A = A + M
}
// Perform arithmetic shift right (ASR)
arithmeticShiftRight(A, Q, &Q_1, n);
}
// Print the final result in binary (A and Q combined)
}
printf("\n");
// Combine A and Q for the final result
int result[16];
for (int i = 0; i < n; i++) {
result[i] = Q[i];
}
for (int i = 0; i < n; i++) {
result[i + n] = A[i];
}
// Convert the result from binary to decimal
int decimalResult = binaryToDecimal(result, 2 * n);
printf("Result in decimal: %d\n", decimalResult);
}
int main() {
boothsAlgorithm(M, Q, n);
return 0;
}
OUTPUT
Enter multiplicand (signed integer): -6
Enter multiplier (signed integer): 3
Booth's Algorithm:
Result in binary: 110111110010
Result in decimal: -18
}
int one[8] = {0, 0, 0, 0, 0, 0, 0, 1}; // To add 1 for two's complement
binaryAddition(temp, one, n); // Add 1 to get 2's complement
binaryAddition(A, temp, n); // Perform binary addition
}
// Restoring Division Algorithm
OUTPUT:
Enter dividend (unsigned integer): 25
Enter divisor (unsigned integer): 3
Quotient in binary: 00001000
Remainder in binary: 00000001
Quotient in decimal: 8
Remainder in decimal: 1
int carry = 0;
for (int i = n - 1; i >= 0; i--) {
int sum = A[i] + B[i] + carry;
A[i] = sum % 2;
carry = sum / 2;
}
}
// Function to perform binary subtraction (A = A - M)
void binarySubtraction(int A[], int M[], int n) {
int temp[n];
// Compute 2's complement of M
for (int i = 0; i < n; i++) {
}
// Non-Restoring Division Algorithm
void nonRestoringDivision(int dividend[], int divisor[], int Q[], int n) {
int A[8] = {0}; // Initialize A to 0
for (int i = 0; i < n; i++) {
// Step 1: Shift left A and Q
shiftLeft(A, Q, n);
if (A[0] == 0) {
binarySubtraction(A, divisor, n);
} else {
// Step 3: A = A + M (if A is negative)
binaryAddition(A, divisor, n);
}
}
// Final correction if A is negative, add divisor back to A
if (A[0] == 1) {
binaryAddition(A, divisor, n);
}
// Print the final result
printf("\n");
// Convert the quotient and remainder to decimal
unsigned int quotient = binaryToDecimal(Q, n);
unsigned int remainder = binaryToDecimal(A, n);
printf("Quotient in decimal: %u\n", quotient);
printf("Remainder in decimal: %u\n", remainder);
}
int main() {
int dividend[8], divisor[8], Q[8]; // Dividend, divisor, quotient
unsigned int dividend_dec, divisor_dec;
int n = 8; // Number of bits for binary numbers (8-bit unsigned)
// Input unsigned dividend and divisor
10.Write assembly language code for A+B*(C-D) using various instruction formats in
MASMor any open-source assembler
.model small
.stack 100h
.data
A dw 10 ; Define A = 10
B dw 5 ; Define B = 5
C dw 8 ; Define C = 8
D dw 3 ; Define D = 3
result dw 0 ; To store the final result
.code
main proc
; Multiply B with (C - D)
mov bx, B ; BX = B
imul bx ; AX = B * (C - D), result in AX
; Add A to the result of the multiplication
add ax, A ; AX = A + B * (C - D)
end main
output
A = 10, B = 5, C = 8, D = 3
A + B * (C - D) = 10 + 5 * (8 - 3) = 10 + 5 * 5 = 35
Write assembly language code for A+B*C using various addressing modes in MASM or any
open-source assembler.
.model small
.stack 100h
.data
A dw 10 ; Define A = 10
B dw 5 ; Define B = 5
C dw 8 ; Define C = 8
result dw 0 ; To store the final result
.code
main proc
; Set up the data segment
int 21h
main endp
end main
output
A = 10, B = 5, C = 8
A + B * C = 10 + 5 * 8 = 10 + 40 = 50