void displayMenu() {
printf("Menu Options:\n");
printf("1. Resistance Calculations\n");
printf("2. Voltage Calculations\n");
printf("3. Current Calculations\n");
printf("4. Exit Program\n");
}
int main() {
int choice = 0;
float voltage = 0.0, current = 0.0;
int resistance = 0;
while (choice != 4) {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: // Resistance Calculation
printf("Please enter the applied voltage (V): ");
scanf("%f", &voltage);
printf("Please enter the current (mA): ");
scanf("%f", ¤t);
if (current == 0) {
printf("You cannot enter a current of zero.\n");
} else if (current < 0 || voltage < 0) {
printf("Negative numbers are not allowed!\n");
} else {
float resistance = voltage / (current / 1000);
printf("The resistance is %.4f ohms.\n", resistance);
}
break;
case 2: // Voltage Calculation
printf("Please enter the resistance (R): ");
scanf("%d", &resistance);
printf("Please enter the current (mA): ");
scanf("%f", ¤t);
if (resistance < 0 || current < 0) {
printf("Negative numbers are not allowed!\n");
} else {
float voltage = (resistance * (current / 1000));
printf("The voltage is %.1f volts.\n", voltage);
}
break;
case 3: // Current Calculation
printf("Please enter the resistance (R): ");
scanf("%d", &resistance);
printf("Please enter the voltage (V): ");
scanf("%f", &voltage);
if (resistance == 0) {
printf("You cannot enter a resistance of zero.\n");
} else if (resistance < 0 || voltage < 0) {
printf("Negative numbers are not allowed!\n");
} else {
float current = (voltage / resistance) * 1000;
printf("The current is %.1f milli amps.\n", current);
}
break;
case 4: // Exit
printf("Quitting...\n");
break;
default: // Invalid choice
printf("An invalid menu option.\n");
break;
}
}
return 0;
}