This question is about Repetition and Loop Statements. Modify the Fahrenheit to
ID: 3636362 • Letter: T
Question
This question is about Repetition and Loop Statements.
Modify the Fahrenheit to Celsius conversion program which is in below by adding to it a loop statement (you may either choose while, for, or do-while) to enable the user to enter many temperatures to convert. After converting each temperature, the program will ask the user if he would like to enter another one and if he answers yes, the program will ask him to enter the next temperature to convert, otherwise, the program terminates.
Here is a sample run of the program (the underlined is data entered by the user):
Please enter the temperature in Fahrenheit> 37.6
The corresponding temperature in Celsius is 3.1
Would you like to enter another one (Y/N): Y
Please enter the temperature in Fahrenheit> 90.5
The corresponding temperature in Celsius is 32.5
Would you like to enter another one (Y/N): Y
Please enter the temperature in Fahrenheit> 120.7
The corresponding temperature in Celsius is 49.3
Would you like to enter another one (Y/N): N
Thank you for using the conversion program. Have a nice day!
The program which need to be modifed:
#include <stdio.h>
float fahrenheitToCelsius(float Fahrenheit);
int main(void)
{
float Fahrenheit;
float Celsius;
printf("Please enter the temperature in Fahrenheit >");
scanf("%f",& Fahrenheit);
printf("The corresponding temperature in Celsius is %2.1f ", fahrenheitToCelsius(Fahrenheit));
}
float fahrenheitToCelsius(float Fahrenheit) {
float Celsius;
Celsius= (Fahrenheit - 32)* 5/9;
return(Celsius);
}
Explanation / Answer
#include <stdio.h>
using namespace std;
int main(void){
bool finished = false;
int i=0;
while(!finished){
String Continue = true;
if(i!=0){
printf("Would you like to enter another one? (Y/N)");
cin >> continue;
}
if(Continue=="Y"){
float Fahrenheit;
printf("Please enter the temperature in Fahrenheit>");
scanf("%f", & Fahrenheit);
printf("The corresponding temperature in Celsius is %2.1f ", fahrenheitToCelsius(float Fahrenheit));
}
i++;
if(Continue=="N"){
finished = true;
printf("Thank you for using the conversion program. Have a nice day!");
}
}
}
float fahrenheitToCelsius(float Fahrenheit) {
float Celsius;
Celsius= (Fahrenheit - 32)* 5/9;
return(Celsius);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.