Step 4 - Calculating speed
Next, let’s calculate the current average speed.
What is your name: Andrew
Hi Andrew.
How far have you travelled so far? Enter km: 5.2How long has it taken? Enter minutes: 7
Your average speed is 44.57142857142857 km/h-
New variables and constants?
We will need to store the speed. We can calculate this as
distancedivided by,timedivided by 60. Where 60 represents minutes per hour.We can track the speed by adding a
speedvariable, but we should also add aMINUTES_PER_HOURconstant to make this meaningful in our code. -
Add the code to calculate the speed and output the speed.
- using static System.Convert;using static SplashKitSDK.SplashKit;const int MINUTES_PER_HOUR = 60;string name;string userInput;double distance, time;double speed;Write("What is your name: ");name = ReadLine();WriteLine();WriteLine($"Hi {name}.");WriteLine();Write("How far have you travelled so far? Enter km: ");userInput = ReadLine();distance = ToDouble(userInput);Write("How long has it taken? Enter minutes: ");userInput = ReadLine();time = ToDouble(userInput);speed = distance / (time / MINUTES_PER_HOUR);WriteLine();WriteLine($"Your average speed is {speed} km/h");
-
-
Compile and run your program. Check the output matches what we are after.