Float, Speed Units - C# Programming Exercise

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#.

 Category

Basic Data Types

 Exercise

Float, Speed Units

 Objective

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).

 Write Your C# Exercise

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
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Sphere, float

    In this C# exercise, you are required to write a program that calculates the surface area and volume of a sphere, given its radius. The formulas provided are as follo...

  •  Vowel - switch

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and, using the switch statement, determines whether the symbol entered is a ...

  •  Vowel - if

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is a vowel (in lowercase), a digit, or any other ...

  •  Triangle, NorthEast

    In this C# exercise, you are asked to write a program that prompts the user for a width and then displays a triangle where the base has the number of asterisks corresponding...

  •  Prime factors

    In this C# exercise, you are asked to write a program that takes a number entered by the user and displays it as a product of its prime factors. Prime factors are numbers th...

  •  If, symbols

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is an uppercase vowel, a lowercase vowel, a digit...