C# Program to Calculate Distance, Speed and Time
Posted by Superadmin on August 10 2022 14:44:42

C# Program to Calculate Distance, Speed and Time

 

 

This is a C# Program to calculate the distance travelled by reading speed and time.

Problem Description

This C# Program Calculates the Distance Travelled by Reading Speed and Time.

Problem Solution

Here distance is calculated by multiplying speed and time.

Program/Source Code

Here is source code of the C# Program to Calculate the Distance Travelled by Reading Speed and Time. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Calculate the Distance Travelled by Reading Speed and Time
 */
using System;
class program
{
    public static void Main()
    {
        int speed, distance, time;
        Console.WriteLine("Enter the Speed(km/hr) : ");
        speed = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter the Time(hrs) : ");
        time = Convert.ToInt32(Console.ReadLine());
        distance = speed * time;
        Console.WriteLine("Distance Travelled (kms) : " + distance);
        Console.ReadLine();
    }
}
Program Explanation

In this C# program, library function defined in <math.h> header file is used. We are reading the Speed(km/hr) and the time(hrs) using ‘speed’ and ‘time’ variables respectively. The following formula is used to compute the distance

 

 Distance = Speed * Time
Runtime Test Cases
 
Enter the Speed(km/hr) : 5
Enter the Time(hrs) : 4
Distance Travelled (kms) : 20