Convert Fahrenheit to celsius and celsius to Fahrenheit - C++ Data Structure

C++ examples for Data Structure:Algorithm

Description

Convert Fahrenheit to celsius and celsius to Fahrenheit

Demo Code

#include <iomanip>
#include <iostream>

double celsius(double);
double Fahrenheit(double);

int main(int argc, const char *argv[]) {
    std::cout << "Fahrenheit equivalent of Celsius 0 to 100\n" << std::endl;
    std::cout << "Celsius\tFahrenheit" << std::fixed << std::setprecision(1) << std::endl;

    for (double c = 0.0; c <= 100.0; c++) {
        std::cout << c << "\t" << Fahrenheit(c) << std::endl;
    }/* w  ww.j a  v a  2s. c om*/

    // Fahrenheit TO CELSIUS
    std::cout << "\nCelsius equivalent of Fahrenheit 32 to 212\n" << std::endl;

    std::cout << "Fahrenheit\tCelsius" << std::fixed << std::setprecision(1) << std::endl;

    for (int f = 32; f <= 212; f++) {
        std::cout << f << "\t" << celsius(f) << std::endl;
    }

    return 0;
}
// convert Fahrenheit to celsius
double celsius(double f) { 
   return (f - 32) * 5 / 9; 
}
// convert celsius to Fahrenheit
double Fahrenheit(double c) { 
   return c * 9 / 5 + 32; 
}

Result


Related Tutorials