C PROGRAMMING PROGRAM • SWITCH & MENU PROGRAMS
Create a Temperature Converter Menu Using switch
Learn how to create a temperature converter menu using switch using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
1 25
Sample output
Fahrenheit = 77.00
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int choice;
double value;
printf("1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius\nChoose: ");
scanf("%d", &choice);
printf("Enter temperature: ");
scanf("%lf", &value);
switch (choice) {
case 1: printf("Fahrenheit = %.2f\n", value * 9.0 / 5.0 + 32.0); break;
case 2: printf("Celsius = %.2f\n", (value - 32.0) * 5.0 / 9.0); break;
default: puts("Invalid choice.");
}
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Fahrenheit = 77.00
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Use the menu choice to select one of the two temperature-conversion formulas.
- Display the computed result.
Use the menu choice to select one of the two temperature-conversion formulas.
EFFICIENCY
Time and space complexity
Time complexity
O(1)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
