Home » .Net » C# programs

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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

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