Lecture - 3 - Arithmetic - Operators - in - C++
Lecture - 3 - Arithmetic - Operators - in - C++
Expression is evaluated and its value is assigned to the variable on the left side
In C++, = is called the assignment operator.
Write a program that takes as input a given length expressed in feet and inches and convert and outputs
the length in centimeters
Problem analysis:
Input: length in feet and inches
o Lengths are given in feet and inches.
o Convert the length in feet and inches to all inches:
Multiply the number of feet by 12
Add given inches
o One inch is equal to 2.54 centimeters
Output: equivalent length in centimeters
o Program computes the equivalent length in centimeters
Needed variables
int feet; //variable to hold given feet
int inches; //variable to hold given inches
int totalInches; //variable to hold total inches
double centimeters; //variable to hold length in centimeters
Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;
Programming Example: Body of the Function
• The body of the function main has the following form:
int main ()
{
declare variables
statements
return 0;
}