Write a function that converts bits to bytes, bytes to bits. Call each function inside a switch case statement. Note: 1 byte = 8 bits.
Wed May 01 2024 06:49:57 GMT+0000 (Coordinated Universal Time)
Saved by
@JC
#include <stdio.h>
int byteBits(int bytes) {
return bytes*8;
}
int bitsByte(int bits) {
return bits/8;
}
int main() {
int num, val, result;
printf("Press 1 if Byte to Bits\nPress 2 if Bits to Byte\nPress 0 if Cancel\n\n");
printf("Please enter a number [1, 2 or 0]: ");
scanf("%d", &num);
switch (num) {
case 1:
printf("Enter the number of bytes: ");
scanf("%d", &val);
result = byteBits(val);
printf("%d bytes = %d bits\n", val, result);
break;
case 2:
printf("Enter the number of bits: ");
scanf("%d", &val);
result = bitsByte(val);
printf("%d bits = %d bytes\n", val, result);
break;
case 0:
printf("Canceled\n");
break;
default:
printf("Invalid. Please try again.\n");
}
return 0;
}
content_copyCOPY
Comments