C# program to calculate the traveled distance based on speed and time

Here, we are going to learn how to calculate the traveled distance based on speed and time in C#?
Submitted by Nidhi, on September 22, 2020

Here we will read the value of speed and time then calculate the traveled distance and print the calculated distance on the console screen.

Program:

The source code to calculate the traveled distance based on speed and time is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to calculate the traveled distance 
//based on speed and time.

using System;

class Demo
{
    static int CalculatDistance(int speed, int time)
    {
        int distance = 0;

        distance = speed * time;

        return distance;
    }
    static void Main(string[] args)
    {
        int speed           =   0;
        int timeInHours     =   0;
        int distance        =   0;

        Console.Write("Enter time in hours: ");
        timeInHours = int.Parse(Console.ReadLine());

        Console.Write("Enter speed in km/hours: ");
        speed = int.Parse(Console.ReadLine());
        
        distance = CalculatDistance(speed, timeInHours);
        Console.WriteLine("Traveled distance in km: " + distance);
    }
}

Output:

Enter time in hours: 2
Enter speed in km/hours: 60
Traveled distance in km: 120
Press any key to continue . . .

Explanation:

Here, we created a Distance class that contains two methods CaclulatDistance() and Main().

In the CalculateDistance() method, we calculated traveled distance using the below formula,
distance = speed * time;

The Main() method is the entry point for the program, here we read the value of time and distance and then calculate the traveled distance using CalculateDistance() method and print the result on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.