Tuesday, March 18, 2014

LoopStructure_Ex_1 (Wk 6) Feb 14, 2014

Ex 1: Use a for loop. Distance is the product of speed and time: (Distance = Speed * Time). Request the vehicle's speed (miles/hour) from user. Request the amount of time it has traveled (in hours). Display Distance and Time for every hour between the first hour and the time traveled. (Display in a table look like).
        static void Main(string[] args)
        {
            Console.Write("Enter your speed mph: ");
            double speed = double.Parse(Console.ReadLine());

            Console.Write("Enter your traveled time: ");
            double time = double.Parse(Console.ReadLine());
            for (int hr = 1; hr <= time; hr++)
            {
                Console.WriteLine("{0, -4} hour, {1, -4} miles.", hr, speed * hr);
                 //-4 is for spacing
            }
            Console.ReadLine();
        }

No comments:

Post a Comment