0% found this document useful (0 votes)
35 views5 pages

Problem 1

The document contains 3 programming problems and solutions. Problem 1 takes user input of temperatures in Fahrenheit and Celsius and determines which is higher. Problem 2 simulates a guessing game where the user tries to guess a "magic number" and is told if their guess is too high or low. Problem 3 takes a wavelength input and outputs the corresponding spectral color, or indicates if it is outside the visible spectrum.

Uploaded by

Stephen Yang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

Problem 1

The document contains 3 programming problems and solutions. Problem 1 takes user input of temperatures in Fahrenheit and Celsius and determines which is higher. Problem 2 simulates a guessing game where the user tries to guess a "magic number" and is told if their guess is too high or low. Problem 3 takes a wavelength input and outputs the corresponding spectral color, or indicates if it is outside the visible spectrum.

Uploaded by

Stephen Yang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

11:15M MC4-10/12/18 Stephen Yang

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

Converts Celsius to Fahrenheit

Calculates difference between


temperatures

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

INPUT: Asks user if


they want to play

INPUT: User’s guess

OUTPUT: If guess is
too low, too high, or
If user puts in 1 the magic number

INPUT: Asks user if


they want to play

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

You might also like