119k views
0 votes
Write a program that will convert a distance provided in inches to a distance in yards, feet and inches. The program will ask the user to input a distance in inches as a whole number. It will convert the distance provided into yards, feet and inches. Then it will display the distance in yards, feet and inches. For example, the program will convert 50 inches into 1 yard, 1 ft and 2 inches. See the Testing section for test data.

User Imgnx
by
4.3k points

1 Answer

4 votes

Answer:

Here is the C++ program:

#include <iostream> // to use input output functions

using namespace std; //to access objects like cin cout

int main() { //start of main method

int inches, feet, yards; // declare variables to hold values of inches, feet and yards

cout << "Enter distance in inches: " //prompts user to enter distance in inches

cin >> inches; //reads value of inches from user

feet = inches/12; // divides value of inches (performs integer division) by 12 and store result in feet

inches = inches%12; //takes modulo of inches with 12 and stores the result in inches variable

yards = feet / 3; // divides value of feet with 3 and stores result in yards

feet = feet % 3; //take modulo of value of feet with 3 and stores result in feet variable

cout<<"yards: "<<yards<<endl<<"feet: "<<feet<<endl<<"inches: "<<inches; }

//displays the value of inches into yards feet and inches

Step-by-step explanation:

I will explain the program with an example:

Suppose the user enters 50 as distance.

inches = 50

feet = inches/12; this statement becomes:

feet = 50/12

The answer of division is 4.166667 but as we are doing integer division so there is no fractional part rather it is represented as whole number and remainder is ignored. So,

feet = 50/12

feet = 4

inches = inches%12; next is this statement which becomes:

inches = 50 % 12

% operator basically computes the remainder. So when 50 is divided by 12 it gives the quotient 4 in integer division and gives 2 as remainder. But we need the value of remainder so,

inches = 50 % 12

inches = 2

yards = feet / 3; next is this statement which becomes:

yards = 4 / 3

Since the value of feet computed in above steps is 4 so this value is divided by 3.

4/3 gives 1.333333 but as we are doing integer division so there is no fractional part rather it is represented as whole number and remainder is ignored. So,

yards = 4 / 3

yards = 1

feet = feet % 3; next is this statement which becomes:

feet = 4 % 3

% operator basically computes the remainder. So when 4 is divided by 3 it gives the quotient 1 in integer division and gives 1 as remainder. But we need the value of remainder so,

feet = 4 % 3

feet = 1

Hence the input distance in inches i.e. 50 is converted to 1 yard, 1 ft and 2 inches as:

yards = 1

feet = 1

inches = 2

If you want to make the program more interactive by dislaying the value of yards, feet and inches as singular and plural then use these if statement in main() function:

if(yards<=1) //checks if value of yard is 1 or less

cout<<yards<<" yard"; //displays 1 yard in singular

else //if value of yard is greater than 1

cout<<yards<<" yards"; //place an "s" with yard to represent value greater than 1

cout<<endl;

if(feet<=1) //checks if value of feet is 1 or less

cout<<feet<<" foot"; //display the value with "foot" representing singular value

else //if value of feet is greater than 1

cout<<feet<<" feet"; //display the value with "feet"

cout<<endl;

if(inches<=1) //checks if value of inches is 1 or less

cout<<inches<<" inch"; //displays 1 inch in singular

else //if value of inches is greater than 1

cout<<inches<<" inches"; //place an "s" with yard to represent value greater than 1

The output of the program is attached.

Write a program that will convert a distance provided in inches to a distance in yards-example-1
User Tanmoy Sarker
by
4.6k points