Skip to content

Commit 49d2dea

Browse files
authored
Merge pull request #252 from abhishekmail-prog/main
Added TemperatureConverter.cpp to Playground
2 parents 1e78159 + 3e26a35 commit 49d2dea

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
==========================================================================
3+
Temperature Converter
4+
5+
Author: Abhishek
6+
Language: C++
7+
Repository: Polyglot-Calculators
8+
9+
Description:
10+
A menu-driven program that converts temperatures
11+
between Celsius, Fahrenheit, and Kelvin.
12+
13+
Conversions Supported:
14+
1. Celsius to Fahrenheit
15+
2. Fahrenheit to Celsius
16+
3. Celsius to Kelvin
17+
4. Kelvin to Celsius
18+
==========================================================================
19+
*/
20+
21+
#include <iostream>
22+
using namespace std;
23+
24+
double celsiusToFahrenheit(double c);
25+
double fahrenheitToCelsius(double f);
26+
double celsiusToKelvin(double c);
27+
double kelvinToCelsius(double k);
28+
29+
int main() {
30+
31+
int choice;
32+
double value;
33+
34+
do {
35+
cout << "\n===== Temperature Converter =====\n";
36+
cout << "1. Celsius to Fahrenheit\n";
37+
cout << "2. Fahrenheit to Celsius\n";
38+
cout << "3. Celsius to Kelvin\n";
39+
cout << "4. Kelvin to Celsius\n";
40+
cout << "5. Exit\n";
41+
cout << "Enter your choice: ";
42+
cin >> choice;
43+
44+
if(choice >= 1 && choice <= 4) {
45+
cout << "Enter temperature value: ";
46+
cin >> value;
47+
}
48+
49+
switch(choice) {
50+
51+
case 1:
52+
cout << "Result: " << celsiusToFahrenheit(value) << " F\n";
53+
break;
54+
55+
case 2:
56+
cout << "Result: " << fahrenheitToCelsius(value) << " C\n";
57+
break;
58+
59+
case 3:
60+
cout << "Result: " << celsiusToKelvin(value) << " K\n";
61+
break;
62+
63+
case 4:
64+
if(value < 0) {
65+
cout << "Error: Kelvin cannot be negative.\n";
66+
} else {
67+
cout << "Result: " << kelvinToCelsius(value) << " C\n";
68+
}
69+
break;
70+
71+
case 5:
72+
cout << "Exiting program.\n";
73+
break;
74+
75+
default:
76+
cout << "Invalid choice. Try again.\n";
77+
}
78+
79+
} while(choice != 5);
80+
81+
return 0;
82+
}
83+
84+
85+
// Function definitions
86+
87+
double celsiusToFahrenheit(double c) {
88+
return (c * 9/5) + 32;
89+
}
90+
91+
double fahrenheitToCelsius(double f) {
92+
return (f - 32) * 5/9;
93+
}
94+
95+
double celsiusToKelvin(double c) {
96+
return c + 273.15;
97+
}
98+
99+
double kelvinToCelsius(double k) {
100+
return k - 273.15;
101+
}

0 commit comments

Comments
 (0)