C# Exercise: 58 Float, speed units

In this C# exercise, you are asked to write a program that prompts the user for two important pieces of information: the distance in meters and the time taken, split into three parts: hours, minutes, and seconds. Using this information, the program will calculate the speed in three different units: meters per second, kilometers per hour, and miles per hour. As a hint, remember that 1 mile equals 1609 meters. This exercise is crucial for learning how to work with unit conversions and mathematical calculations in C#. Additionally, it will help you become familiar with user input handling and performing mathematical operations within a program. Pay close attention to the accuracy of conversions to avoid errors in the final calculations.
Such exercises are particularly useful in applications related to measurement, sports tracking, or performance analysis. If you want to improve your programming skills, this exercise will provide excellent practice with variables, operators, and conversions in C#.



 Lesson

Basic Data Types

 Exercise

Float, speed units

 Objetive

Write a C# program to ask the user for a distance (in meters) and the time taken (as three numbers: hours, minutes, seconds), and display the speed, in meters per second, kilometers per hour, and miles per hour (hint: 1 mile = 1609 meters).

 Example Code

using System;  // Import the System namespace to use basic classes like Console

class Program  // Define the main class of the program
{
    static void Main()  // The entry point of the program
    {
        // Ask the user for the distance in meters
        Console.Write("Enter the distance in meters: ");  // Display prompt for distance
        float distance = float.Parse(Console.ReadLine());  // Read and parse the user's input for distance

        // Ask the user for the time in hours, minutes, and seconds
        Console.Write("Enter the time in hours: ");  // Prompt for hours
        int hours = int.Parse(Console.ReadLine());  // Read and parse the hours

        Console.Write("Enter the time in minutes: ");  // Prompt for minutes
        int minutes = int.Parse(Console.ReadLine());  // Read and parse the minutes

        Console.Write("Enter the time in seconds: ");  // Prompt for seconds
        int seconds = int.Parse(Console.ReadLine());  // Read and parse the seconds

        // Calculate the total time in seconds
        int totalTimeInSeconds = (hours * 3600) + (minutes * 60) + seconds;  // Convert hours and minutes to seconds and add the seconds

        // Calculate speed in meters per second
        float speedInMetersPerSecond = distance / totalTimeInSeconds;  // Speed = distance / time

        // Calculate speed in kilometers per hour
        float speedInKilometersPerHour = (distance / 1000) / (totalTimeInSeconds / 3600f);  // Speed in km/h = (distance in meters / 1000) / (time in seconds / 3600)

        // Calculate speed in miles per hour
        float speedInMilesPerHour = (distance / 1609) / (totalTimeInSeconds / 3600f);  // Speed in mph = (distance in meters / 1609) / (time in seconds / 3600)

        // Display the calculated speeds
        Console.WriteLine($"Speed in meters per second: {speedInMetersPerSecond:F2} m/s");  // Display speed in meters per second, formatted to 2 decimal places
        Console.WriteLine($"Speed in kilometers per hour: {speedInKilometersPerHour:F2} km/h");  // Display speed in km/h, formatted to 2 decimal places
        Console.WriteLine($"Speed in miles per hour: {speedInMilesPerHour:F2} mph");  // Display speed in mph, formatted to 2 decimal places
    }
}


More C# Exercises of Basic Data Types

 Char
Write a C# program to ask the user for three letters and display them in reverse order....
 Triangle
Write a C# program that prompts for a symbol and a width, and displays a triangle of that width, using that number for the inner symbol, as in this ex...
 Password as string
Write a C# program to ask the user for their username and password (both should be strings) and repeat it as many times as necessary until the entered...
 Password 5 attempts
Write a C# program that prompts the user for their username and password. Both should be strings. After 5 incorrect attempts, the user will be rejecte...
 Calculator - if
Write a C# program that asks the user for two numbers and an operation to perform on them (+,-,*,x,/) and displays the result of that operation, as in...
 Calculator - switch
Write a C# program that asks the user for two numbers and an operation to perform on them (+,-,*,x,/) and displays the result of that operation, as in...
 Double
Write a C# program that calculate the perimeter, area, and diagonal of a rectangle, given its width and height. (Hint: use y = Math.Sqrt(x) to calc...
 Calculate values of a function
Write a C# program in C# to display certain values of the function y = x^2 - 2x + 1 (using integer numbers for x, ranging from -10 to +10)...
 Display a function
Write a C# program to "draw" the graphic of the function y = (x-4)2 for integer values of x ranging -1 to 8. It will show as many asterisks on screen ...
 Sphere, float
Write a C# program that calculate the surface area and volume of a sphere, given its radius (surface area = 4 * pi * radius squared; volume = 4/3 * pi...
 Vowel - switch
Write a C# program to ask the user for a symbol and respond whether it is a vowel (in lowercase), a digit, or any other symbol, using "switch"....
 Vowel - if
Write a C# program to ask the user for a symbol and respond if it's a vowel (in lowercase), a digit, or any other symbol, using "if"....
 Triangle, NorthEast
Write a C# program which asks for a width, and displays a triangle like this one: Enter the desired width: 5 ***** _**** __*** ___** ____*...
 Prime factors
Write a C# program that displays a number (entered by the user) as a product of its prime factors. For example, 60 = 2 · 2 · 3 · 5 (Hint: it can be...
 If, symbols
Write a C# program to ask the user for a symbol and answer if is an uppercase vowel, a lowercase vowel, a digit or any other symbol, using "if"....
 Char + for
Write a C# program to write the letters "B" to "N" (uppercase), using "for"...
 Double, approximation of Pi
Write a C# program to calculate an approximation for PI using the expression: pi/4 = 1/1 - 1/3 + 1/5 -1/7 + 1/9 - 1/11 + 1/13 ... The user will ...
 Perimeter Area
Write a C# program to calculate the perimeter, area and diagonal of a rectangle from its width and height (perimeter = sum of the four sides, area = b...
 Hexadecimal and binary
Write a C# program to ask the user for a number an display it both in hexadecimal and binary. It must repeat until the user enters 0....
 Binary
Write a C# program that asks the user for a decimal number and displays its equivalent in binary form. It should be repeated until the user enters the...
 Conditional and boolean
Write a C# program that uses the conditional operator to give a boolean variable named "bothEven" the value "true" if two numbers entered by the user ...
 Exceptions V2
Write a C# program to ask the user for a real number and display its square root. Errors must be trapped using "try..catch". Does it behave as you ...