In: Computer Science
this is a C++ class
Topics
Numeric Input (Whole Numbers)
Assignment
Finding Quotient
Finding Remainder
Numeric Output (Whole Numbers)
Description
Write a program that will convert a distance entered 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 entered into yards, feet and inches. Then it will display the distance in yards, feet and inches.
For example, the program will convert 5o inches into 1 yard, 1 ft and 2 inches. See the Testing section for test data.
Requirement
Do this exercise using only three variables. See Sample Code section below.
Test Data
Use the test data in the Test Run below.
Test Run
(User input is shown in bolds).
Enter distance in inches:
140
The distance of 140 inches is the same as the distance of 3 yards, 2 feet and 8 inches
Submit
Final one line output
Source code
Discussion
There is a difference between decimal division and integer division.
They both use the same symbol “/ “.
But the meaning of the symbol “/ “ is different depending upon the context it is used in.
Decimal Division:
If one or both the operands around the division symbol are decimal variables or constants, a decimal division is performed.
In a decimal division, the answer is a decimal number. The remainder is irrelevant.
Integer Division:
If both the operands around the division symbol are whole number variables or constants, an integer division is done.
In an integer division, the whole number quotient is the answer, the remainder is ignored.
To find remainder in an integer division, use the remainder operator ( % ).
The remainder operator works only when both the operands are whole number variables or constants.
Remember, in an integer division, the / operator returns the quotient and the % operator returns the remainder.
Examples:
14/4; //Integer division. Answer 3.
14/4.0; //Decimal division. Answer 3.5
14.0/4.0; //Decimal division. Answer 3.5
14 % 4; //Remainder operator. Answer 2
14 % 4.0; //Illegal. Remainder operator not available for decimal values.
14.0 % 4; //Illegal
double d = 14/4.0; //d is 3.5
double d = 14.0/4.0; //d is 3.5
double d2 = 14/4; //d2 is 3
int i = 14/4; //i is 3
double x = 14;
double z = x / 4; //z is 3.5
Sample Code
int inches, feet, yards;
cout << "Enter distance in inches" << endl;
cin >> inches; // assume user enters 50
// Convert inches into feet and inches.
//The quotient operation below will return feet
feet = inches/12; //This integer division computes quotient. So feet is now 4. Remainder is ignored.
//The remainder operation below will return the inches.
inches = inches%12; //This computes the remainder. So, inches is now 2. Quotient is ignored.
//Conver feet into yards and feet.
//Repeat the above process.
//Find yards (from the feet above) by using quotient operator ( / ).
yards = feet / 3; //yards is now 1
//Find the remaining feet (from the feet above) by using remainder operator (%).
feet = feet % 3; //feet is now 1
Thanks for the question.
Here is the completed code for this problem.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer.
Thanks
===========================================================================
#include <iostream>
using namespace std;
int main(){
int inches, feet, yards;
cout << "Enter distance in inches: ";
cin >> inches; // assume user enters 50
int starting_inches=inches;
// 36 inches = 1 yard
yards = inches/36;
inches = inches%36;
// 12 inches = 1 feet
feet = inches/12;
inches = inches%12;
cout<<starting_inches<<" inches is
"<<yards<<" yards "<<feet<<" feet(s) and
"<<inches<<" inches."<<endl;
}
=====================================================================