Temperature Converter – C++ Program
This C++ program converts temperatures between Celsius and Fahrenheit. It includes two functions:
celsiusToFahrenheit(double celsius)
: Converts a temperature from Celsius to Fahrenheit.fahrenheitToCelsius(double fahrenheit)
: Converts a temperature from Fahrenheit to Celsius.
C++ Code
// TemperatureConverter.cpp
#include <iostream>
// Function to convert Celsius to Fahrenheit
double celsiusToFahrenheit(double celsius) {
return (celsius * 9.0/5.0) + 32.0;
}
// Function to convert Fahrenheit to Celsius
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0/9.0;
}
int main() {
// Example usage
double celsius = 25;
double fahrenheit = 77;
std::cout << celsius << "°C is equal to " << celsiusToFahrenheit(celsius) << "°F" << std::endl;
std::cout << fahrenheit << "°F is equal to " << fahrenheitToCelsius(fahrenheit) << "°C" << std::endl;
return 0;
}
Explanation
This program consists of the following components:
Functions
- celsiusToFahrenheit(double celsius): This function takes a temperature in Celsius as input and converts it to Fahrenheit using the formula
(celsius * 9.0/5.0) + 32.0
. - fahrenheitToCelsius(double fahrenheit): This function takes a temperature in Fahrenheit as input and converts it to Celsius using the formula
(fahrenheit - 32.0) * 5.0/9.0
.
Main Function
The main
function demonstrates how to use these conversion functions. It initializes two example temperatures, one in Celsius and one in Fahrenheit, and then prints the converted values to the console.