Write a program that declares constants to represent the number of inches, feet, and yards in a mile. Name the constants INCHES_IN_MILE, FEET_IN_MILE, and YARDS_IN_MILE respectively. Also declare a variable named miles to represent a number of miles. Compute and display, with explanatory text, the value in inches, feet, and yards—for example: 4.0 miles is 253440.0 inches, or 21120.0 feet, or 7040.0 yards

Respuesta :

Answer:

The cpp program for conversion is given below.

#include <iostream>

using namespace std;

 

int main() {

 

// declaring and initializing constant variables with standard values

   const  double INCHES_IN_MILE=63360;

   const  double FEET_IN_MILE=5280;

   const  double YARDS_IN_MILE=1760;

   

   // declaring and initializing miles with a random value

   double miles=4.0;

   

// declaring and computing inches in miles

double inch = INCHES_IN_MILE*miles;

 

// declaring and computing feet in miles

double feet = FEET_IN_MILE*miles;

 

// declaring and computing yards in miles

double yard = YARDS_IN_MILE*miles;

 

// displaying all the calculated values

std::cout<<miles<<" miles is "<<inch<<" inches"<<std::endl;

std::cout<<miles<<" miles is "<<feet<<" feet"<<std::endl;

       std::cout<<miles<<" miles is "<<yard<<" yards"<<std::endl;

   

       return 0;      

}

OUTPUT

4 miles is 253440 inches

4 miles is 21120 feet

4 miles is 7040 yards

Explanation:

The steps in the program are as described.

1. All the three constant variables, mentioned in the question, are declared as double and initialized. These variables hold the fixed conversion values for inches, feet and yards in one mile.

2. The variables are declared constant using the keyword, const.

3. The variable to hold value for mile, miles, is declared as double and initialized with a random value.

4. Next, three more variables are declared as double. These variables will hold the calculated values for inches, feet and yards in the given value of mile.

5. The values are calculated for inches, feet and yards for the given value of variable, miles and stored in the variables mentioned in step 3.

6. These values are displayed to the console along with explanations.

7. The program ends with the return statement.

8. The value of variable, miles, is hard coded in the program and not taken from the user.

9. There is no interaction with the user in this program as this is not mentioned in the question.

10. The program works for all numeric value of the variable, miles, since double accepts integers also.

11. The program can be tested by changing the value of variable, miles.