Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Temperature Conversion Using User-Defined Functions, Conditionals, and Loops Wri

ID: 3699120 • Letter: T

Question

Temperature Conversion Using User-Defined Functions, Conditionals, and Loops
Write a program that asks the user to choose between Celsius-to-Fahrenheit conversion or Fahrenheit-to-Celsius conversion. The user will also have the option to quit the program. (You can use 1, 2, and 3 respectively).
If the incorrect choice is typed, display an error message and prompt the user to re-enter his/her choice.
Once the user has chosen which type of conversion he/she wants, the program should prompt the user to enter the temperature value to convert. Finally, display the converted value.
Depending on the choice, use the following formulas accordingly:
Formula to convert from Celsius to Fahrenheit: ((9.0/5.0)*degC)+32.0
Formula to convert from Fahrenheit to Celsius: 5.0*(degF-32.0)/9.0
The process must continue until the user enters the option to quit the program.
You must create two separate functions for the temperature conversion: one for the Celsius-to-Fahrenheit conversion and another one for the Fahrenheit-to-Celsius conversion. This is the only task the functions will perform. The conversions must be done through function calls. The functions must return the converted value to the main script, where result will be printed to 1 decimal place. _______matlab


Explanation / Answer

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% MAIN SCRIPT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

while 1

fprintf('1. Degree Celcius to Fahrenheit ');
fprintf('2. Fahrenheit to Degree Celcius ');
fprintf('3. Exit ');
prompt = 'Enter Your Choice:';
x = input(prompt);
if x==1
prompt = 'Enter temprature in Degree Celcius:';
y = input(prompt);
z = celcius2fahrenheit(y);
fprintf('Temp in Fahrenheit is %.1f ',z); %printing the result rounded off to one decial place
elseif x==2
prompt = 'Enter temprature in Fahrenheit:';
y = input(prompt);
z = fahrenheit2celcius(y);
fprintf('Temp in Degree Celcius is %.1f ',z);   %printing the result rounded off to one decial place
elseif x==3
break;
else
fprintf('Invalid Choice ');
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Celsius to Fahrenheit function

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [output_temp] = celcius2fahrenheit(input_temp)

output_temp = ((9.0/5.0)*input_temp) + 32;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Fahrenheit to Celsius function

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [output_temp] = fahrenheit2celcius(input_temp)

output_temp = (input_temp - 32)*5/9;

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote