-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathconvert_c_to_f.cpp
47 lines (39 loc) · 1.62 KB
/
convert_c_to_f.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
/*******************************************************************************
*
* Program: Convert Celsius To Fahrenheit
*
* Description: Program to convert a temperature in Celsius to a temperature in
* Fahrenheit using C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=bEzG6bpNmRk
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Variables to store temperatures in celsius and fahrenheit. Type double is
// used because temperatures may have decimal places and double type variables
// can store numbers with decimal places.
double celsius, fahrenheit;
// Prompt the user to enter the temperature in celsius
cout << "Enter Temperature (C): ";
// Store the temperature entered into the variable celsius
cin >> celsius;
// Convert the temperature in celsius to the temperature in fahrenheit using
// the formula: https://en.wikipedia.org/wiki/Fahrenheit. Store the
// converted temperature into the variable fahrenheit. Note it is important
// that we divide 9.0/5.0 and not 9/5 as the latter will result in integer
// division (i.e. 1 and not 1.8) which will then result in the formula
// producing incorrect results.
fahrenheit = (9.0/5.0) * celsius + 32;
// Output the temperature in fahrenheit with 2 decimal digits of precision
// using the stream manipulators fixed and setprecision from the iomanip
// library.
cout << "Temperature (F): ";
cout << fixed << setprecision(2) << fahrenheit << endl;
return 0;
}