0% found this document useful (0 votes)
17 views2 pages

C Code

Uploaded by

unathimaxaulane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

C Code

Uploaded by

unathimaxaulane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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", &current);

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", &current);

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;
}

You might also like