The formula to convert Celsius temperature given a Fahrenheit temperature is below: C° =…
- User
The formula to convert Celsius temperature given a Fahrenheit temperature is below: C° = (F° - 32) x (5 / 9) //consider the danger of integer division in your C++ formula Create a C++ program to:
- accept input of a Fahrenheit temperature from the user
- Calculate the Celsius temperature only
- Output the Celsius temperature to the screen.
- Set the precision of the outputted Celsius variable to 4 decimal places.
- Assistant
Here is a C++ program that fulfills the given requirements:
#include <iostream> #include <iomanip> int main() { double fahrenheit; // Accept input of Fahrenheit temperature from the user std::cout << "Enter Fahrenheit temperature: "; std::cin >> fahrenheit; // Calculate the Celsius temperature double celsius = (fahrenheit - 32) * (5.0 / 9.0); // Avoiding integer division // Output the Celsius temperature with precision of 4 decimal places std::cout << std::fixed << std::setprecision(4); // Set precision to 4 decimal places std::cout << "Celsius temperature: " << celsius << std::endl; return 0; }In this program, we use the
std::cinobject to accept the input of Fahrenheit temperature from the user. We then calculate the Celsius temperature using the given formula, making sure to use a floating-point division by using5.0 / 9.0. Finally, we usestd::coutto output the Celsius temperature with a precision of 4 decimal places usingstd::fixedandstd::setprecision(4)manipulators. - User
Consider the following fragment of code: if ( id == 1 ) { cout << "Martha" << endl; } else {
if ( id == 2 || id == 3 ) { cout << "David" <<endl; } else { if ( id == 4 ) { cout << "Green Tea" << endl; } else { cout << "Commit to Complete" << endl; } }} (a) What will the if structure display if the id variable has value 5 (b) What will the if structure display if the id variable has value 3
(c) What will the if structure display if the id variable has value G
- Assistant
(a) If the
idvariable has a value of 5, the if structure will display "Commit to Complete" because none of the if conditions will be true.(b) If the
idvariable has a value of 3, the if structure will display "David" because the conditionid == 2 || id == 3will be true, and it will execute the corresponding statement.(c) If the
idvariable has a value of "G", the behavior is undefined. The if structure is expecting an integer value to compare with, but we are providing a character. In this case, the program may give a compilation error or unpredictable behavior. - User
Create a C++ program to accept input from the user between the char values of A - F or lowercase a - f. Output to the user, using “IF”, "ELSE-IF", and/or "ELSE" statements what the entered value's range is in terms of numerical range i.e. (A or a entered outputs "Your earned grade was 90.0% or greather.") Account for invalid output in the default clause.
- Assistant
Here is a C++ program that fulfills the given requirements:
#include <iostream> int main() { char grade; // Accept input from the user std::cout << "Enter your grade: "; std::cin >> grade; // Check the range of the entered value using if, else if, and else statements if (grade == 'A' || grade == 'a') { std::cout << "Your earned grade was 90.0% or greater." << std::endl; } else if (grade == 'B' || grade == 'b') { std::cout << "Your earned grade was between 80.0% and 89.9%." << std::endl; } else if (grade == 'C' || grade == 'c') { std::cout << "Your earned grade was between 70.0% and 79.9%." << std::endl; } else if (grade == 'D' || grade == 'd') { std::cout << "Your earned grade was between 60.0% and 69.9%." << std::endl; } else if (grade == 'E' || grade == 'e') { std::cout << "Your earned grade was between 50.0% and 59.9%." << std::endl; } else if (grade == 'F' || grade == 'f') { std::cout << "Your earned grade was below 50.0%." << std::endl; } else { std::cout << "Invalid input." << std::endl; } return 0; }