Pgm Description:: To write a program that converts temperatures between Celsius and Fahrenheit using a menu-driven interface.
Pgm Details: This program was developed for the III Semester B.Com (CA) curriculum (2021-2022) at Vidhya Sagar Women's College.
Pgm Logic:
STEP 1: Start the program.
STEP 2: Declare the variables for Celsius (cs) and Fahrenheit (fa) as float, and choice (ch) as integer.
STEP 3: Display a menu with options for Celsius to Fahrenheit conversion, Fahrenheit to Celsius conversion, and an Exit option.
STEP 4: Read the user's choice.
STEP 5: If Choice is 1, read Celsius, calculate Fahrenheit using the formula fa = (cs * 1.8) + 32, and display the result.
STEP 6: If Choice is 2, read Fahrenheit, calculate Celsius using the formula cs = (fa - 32) / 1.8, and display the result.
STEP 7: If the choice is 3 or default, exit the program.
STEP 8: Stop the program.
Program Code:
// Purpose: Temperature conversion between Celsius and Fahrenheit using Switch Case
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
clrscr();
int ch;
float fa, cs;
cout << "\t TEMPERATURE CONVERSION \n\n";
cout << "***Menu***\n1.Celsius to Fahrenheit\n2.Fahrenheit to Celsius";
cout << "\n3.Exit \n\n4.Enter your choice:";
cin >> ch;
switch(ch)
{
case 1:
{
cout << "\nEnter celsius value: ";
cin >> cs;
fa = ((cs * 1.8) + 32);
cout << "\n\n Fahrenheit value=" << fa << "\n";
break;
}
case 2:
{
cout << "\nEnter Fahrenheit value: ";
cin >> fa;
cs = ((fa - 32) / 1.8);
cout << "\n\n Celsius Value=" << cs << "\n";
break;
}
default:
exit(0);
}
getch();
}
Output:
TEMPERATURE CONVERSION
***Menu***
1.Celsius to Fahrenheit
2.Fahrenheit to Celsius
3.Exit
4.Enter your choice:1
Enter celsius value: 30
Fahrenheit Value=86
RESULT: Thus the program has been executed and the output was verified.
Remarks: This program uses legacy C++ headers (iostream.h); if running in modern IDEs like onlineGDB or Code::Blocks, you should use #include <iostream> and using namespace std;.
Program Explanation: The program uses a switch statement to handle the user's selection from a menu. It performs floating-point arithmetic to apply standard conversion formulas based on the user's input.
eBook ‘C++ Lab Programs Collection’ purchase Link: Google Play Store || Google Books
...till the next post, bye-bye & take care