SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Barbara Dolye
Chapter:
Data Types And Expressions
Exercise:
Programming Exercises
Question:2 | ISBN:9781285096261 | Edition: 4

Question

Write a program that converts a mile into its equivalent metric kilometer
measurement. Test the program by performing a compile-time initialization of
10 for the miles value. Display the original miles and the formatted converted
value. Go into your source code and change the initialization value and rerun the
application with a new mile value of 3.5. For an additional challenge, include in
your application a kilometer to miles converter.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgExercise2
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaration
            double milesLength = 10;
            double kilometerLegth;
            //logic 1mile=1.60934 km
            kilometerLegth = milesLength * 1.60934;  //convertion from miles to kilometers

            //display output
            Console.WriteLine("Given Miles: " + "{0:f2}", +milesLength);
            Console.WriteLine("Length from Miles to Kilometer : " + "{0:f2}", +kilometerLegth);
 
        }
    }
}

Output 1:

Given Miles: 10.00
Length from Miles to Kilometer : 16.09

Output 1:

Given Miles: 3.50
Length from Miles to Kilometer : 5.63

-------------------------------------------------------------------------------------------------------------------------------------

Solution for Additional challange:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgExercise2Additional
{
    class Program
    {
        static void Main(string[] args)
        {
            //declaration

            double miles= 10;
            double milesLength;
            double kilometers = 16.09;
            double kilometerLegth;
            //logic 1mile=1.60934 km
                    
            kilometerLegth = miles * 1.60934;  //convertion from miles to kilometers
            milesLength = kilometers / 1.60934; //convertion from kilometers to miles
            //display output
            Console.WriteLine("Given Miles: " + "{0:f2}", +miles);
            Console.WriteLine("Length from Miles to Kilometer : " + "{0:f2}", +kilometerLegth);
            Console.WriteLine("Given kilometers: " + "{0:f2}", +kilometers);
            Console.WriteLine("Length from Kilometers to Miles : " + "{0:f2}", +milesLength);
 
        }
    }
}
Output:

Given Miles: 10.00
Length from Miles to Kilometer : 16.09
Given kilometers: 16.09
Length from Kilometers to Miles : 10.00

0 0

Discussions

Post the discussion to improve the above solution.