Problem 1
Problem 1
Problem 1
% 11:15M MC4-10/12/18 Stephen Yang
% Problem Statement: The purpose of this program is to
% take user input of temperatures in Fahrenheit and Celsius
% and determines which is higher.
clear
clc
% Takes user input of temperatures in F and C
F = input('Please enter temp in F ');
C = input('Please enter temp in C ');
% Converts Celsius temperature to Fahrenheit
C = (C * 9/5) + 32;
% Calculates difference between temperatures
Diff = F - C;
% Determines which temperature is higher or if they're equal
if abs(Diff) < 0.05
disp("The temperatures are equal")
elseif Diff < 0
disp("The Fahrenheit temperature is cooler")
elseif Diff > 0
disp("The Fahrenheit temperature is warmer")
end
Sample Runs
11:15M MC4-10/12/18 Stephen Yang
Flow Chart
START
INPUT: Temperatures
in F and C
OUTPUT: Which
temperature is higher
or if they’re equal
STOP
Problem 2
% 11:15M MC4-10/12/18 Stephen Yang
% Problem Statement: The purpose of this program is to simulate a guessing game
% where the user tries to guess the "magic number." The program
% tells the user if they're too low or high. The user can
% keep playing until they win, or give up.
clear
clc
% Sets the magic number to 5
goal = 5;
% Asks user if they want to play
play = input('Would you like to play a game? Enter 1 for yes and 0 for no: ');
% This runs however many times the user wants to play/guess
while play == 1
% Takes user input on guess
guess = input('Please guess a number! ');
% Outputs if the guess is too low, too high or the magic number
if guess == 5
disp('Congratulations, you have won the guessing game!')
play = input('Would you like to play again? Enter 1 for yes and 0 for no: ');
elseif guess > 5
disp('Aww, not quite right, that is too high!')
play = input('Would you like to give up? Enter 1 for no, 0 for yes: ');
else
disp('Aww, not quite right, that is too low!')
play = input('Would you like to give up? Enter 1 for no, 0 for yes: ');
end
end
11:15M MC4-10/12/18 Stephen Yang
Flow Chart
START
OUTPUT: If guess is
too low, too high, or
If user puts in 1 the magic number
If user puts in 0
STOP
Sample Runs
11:15M MC4-10/12/18 Stephen Yang
Problem 3
% 11:15M MC4-10/12/18 Stephen Yang
% Problem Statement: The purpose of this program is to get user
% input on a wavelength interval and output the spectral color
% that the given wavelength falls, or if it's not within the visible
% spectrum.
clear
clc
% Takes input for a wavelength
wave = input('Please input a wavelength interval: ');
% Outputs the spectral color that the wavelength falls, or if
% it's not in the visible spectrum
if wave < 700 & wave >= 635
disp('Red')
elseif wave < 635 & wave >= 590
disp('Orange')
elseif wave < 590 & wave >= 560
disp('Yellow')
elseif wave < 560 & wave >= 490
disp('Green')
elseif wave < 490 & wave >= 450
disp('Blue')
elseif wave < 450 & wave >= 400
disp('Violet')
else disp('This wavelength is not within the visible spectrum')
end
11:15M MC4-10/12/18 Stephen Yang
Sample Runs
Flow Chart
START
INPUT: Wavelength
OUTPUT: Spectral
Color
STOP