-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
89 lines (76 loc) · 2.5 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
// Function prototypes
void celsiusToFahrenheit(float celsius);
void celsiusToKelvin(float celsius);
void fahrenheitToCelsius(float fahrenheit);
void fahrenheitToKelvin(float fahrenheit);
void kelvinToCelsius(float kelvin);
void kelvinToFahrenheit(float kelvin);
int main() {
int choice;
float temperature;
cout << "Temperature Converter\n";
cout << "1. Celsius to Fahrenheit\n";
cout << "2. Celsius to Kelvin\n";
cout << "3. Fahrenheit to Celsius\n";
cout << "4. Fahrenheit to Kelvin\n";
cout << "5. Kelvin to Celsius\n";
cout << "6. Kelvin to Fahrenheit\n";
cout << "Enter your choice (1-6): ";
cin >> choice;
if (choice < 1 || choice > 6) {
cout << "Invalid choice. Please run the program again and choose a valid option.\n";
return 1;
}
cout << "Enter the temperature: ";
cin >> temperature;
switch (choice) {
case 1:
celsiusToFahrenheit(temperature);
break;
case 2:
celsiusToKelvin(temperature);
break;
case 3:
fahrenheitToCelsius(temperature);
break;
case 4:
fahrenheitToKelvin(temperature);
break;
case 5:
kelvinToCelsius(temperature);
break;
case 6:
kelvinToFahrenheit(temperature);
break;
}
return 0;
}
// Function definitions
void celsiusToFahrenheit(float celsius) {
float fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
cout << celsius << " Celsius is equal to " << fahrenheit << " Fahrenheit.\n";
}
void celsiusToKelvin(float celsius) {
float kelvin = celsius + 273.15;
cout << celsius << " Celsius is equal to " << kelvin << " Kelvin.\n";
}
void fahrenheitToCelsius(float fahrenheit) {
float celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
cout << fahrenheit << " Fahrenheit is equal to " << celsius << " Celsius.\n";
}
void fahrenheitToKelvin(float fahrenheit) {
float celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
float kelvin = celsius + 273.15;
cout << fahrenheit << " Fahrenheit is equal to " << kelvin << " Kelvin.\n";
}
void kelvinToCelsius(float kelvin) {
float celsius = kelvin - 273.15;
cout << kelvin << " Kelvin is equal to " << celsius << " Celsius.\n";
}
void kelvinToFahrenheit(float kelvin) {
float celsius = kelvin - 273.15;
float fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
cout << kelvin << " Kelvin is equal to " << fahrenheit << " Fahrenheit.\n";
}