0% found this document useful (0 votes)
11 views

Programming C Week 5 6

Uploaded by

markham0915
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming C Week 5 6

Uploaded by

markham0915
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

The basic structure of 598194

gramming
Arithmetic Operators

There are many operators for manipulating numeric


values and performing arithmetic operations. C++ offers a multi-
tude of operators for manipulating data (Gaddis, 2009). Gener-
ally, there are three types of operators: unary, binary, and
ternary. These terms reflect the number of operands an
operator requires.
Addition Operator

Returns the sum of its two operands. In the following assign-


ment statement, the variable amount will be assigned the value
12:

Example: amount = 4 + 8;
Subtraction Operator

Returns the value of its right operand subtracted from


its left operand. This statement will assign the value 98 to
temperature:

Example: temperature = 112 − 14;


Multiplication Operator

Returns the product of its two operands. In the


following statement, markUp is assigned the value 3:

Example: markUp = 12 * 0.25;


Division Operator

Returns the quotient of its left operand divided by its


right operand. In the next statement, points is assigned the
value 5:

Example: points = 100 / 20;


Table 1. Fundamental Arithmetic Operators
Example: Suppose we need to write a program that calculates
and displays an employee’s total wages for the week.

The regular hours for the work week are 40, and any hours
worked over 40 are considered overtime. The employee earns $18.25
per hour for regular hours and $27.78 per hour for overtime hours.
The employee has worked 50 hours this week.
Regular wages = base pay rate × regular hours
Overtime wages = overtime pay rate × overtime hours
Total wages = regular wages + overtime wages
Display the total wages
Integer Division

When both operands of a division statement are


integers, the statement will result in integer division. This means
the result of the division will be an integer as well. If there is a
remainder, it will be discarded. For example, look at the
following code:

double number;
number = 5 / 2;
Integer Division

This code divides 5 by 2 and assigns the result to the number


variable. What will be stored in number? You would probably
assume that 2.5 would be stored in number because that is the
result your calculator shows when you divide 5 by 2. However,
that is not what happens when the previous C++ code is
executed. Because the numbers 5 and 2 are both integers, the
fractional part of the result will be thrown away, or truncated.
Integer Division

As a result, the value 2 will be assigned to the number


variable. In the previous code, it doesn’t matter that the number
variable is declared as a double because the fractional part of
the result is discarded before the assignment takes place. In
order for a division operation to return a floating-point value,
one of the operands must be of a floating-point data type. For
example, the previous code could be written as follows:

double number;
number = 5.0 / 2;
Integer Division

In this code, 5.0 is treated as a floating-point number,


so the division operation will return a floating-point number. The
result of the division is 2.5.
Integer Division

Example 1: Suppose you earn $6,000 per month and


you are allowed to contribute a portion of your gross monthly
pay to a retirement plan. You want to determine the amount of
your pay that will go into the plan if you contribute 5 percent, 7
percent, or 10 percent of your gross wages. To make this
determination you write the program shown in below.
Line 11 defines two variables: monthlyPay and
contribution. The monthlyPay variable, which is initialized
with the value 6000.0, holds the amount of your monthly pay.
The contribution variable will hold the amount of a contribution
to the retirement plan. The statements in lines 14 through 16
calculate and display 5 percent of the monthly pay. The
calculation is done in line 14, where the monthlyPay vari-
able is multiplied by 0.05. The result is assigned to the contribu-
tion variable, which is then displayed in line 15. Similar steps
are taken in Lines 18 through 21, which calculate and dis-
play 7 percent of the monthly pay, and lines 24 through 26,
which calculate and display 10 percent of the monthly pay.
Here is line 16, which multiplies basePayRate times regularHours
and stores the result in regularWages :

 regularWages = basePayRate * regularHours;

Here is line 19, which multiplies overtimePayRate times overtimeHours


and stores the result in overtimeWages :

 overtimeWages = overtimePayRate * overtimeHours;

Line 22 adds the regular wages and the overtime wages and stores the
result in totalWages :

 totalWages = regularWages + overtimeWages;

Line 25 displays the message on the screen reporting the week’s wages.
Integer Division

Example 2: Another common calculation is


determining a percentage discount. For example, suppose a
retail business sells an item that is regularly priced at $59.95
and is planning to have a sale where the item’s price will be
reduced by 20 percent. You have been asked to write a
program to calculate the sale price of the item. To determine the
sale price you perform two calculations:
Integer Division

• First, you get the amount of the discount, which is 20 percent


of the item’s regular price.

• Second, you subtract the discount amount from the item’s


regular price. This gives you the sale price.
Line 11 defines three variables. The regularPrice
variable holds the item’s regular price, and is initialized with the
value 59.95. The discount variable will hold the amount of the
discount once it is calculated. The salePrice variable will hold
the item’s sale price. Line 14 calculates the amount of the 20
percent discount by multiplying regularPrice by 0.2. The result
is stored in the discount variable. Line 18 calculates the sale
price by subtracting discount from regularPrice. The result is
stored in the salePrice variable. The cout statements in lines 21
through 23 display the item’s regular price, the amount of the
discount, and the sale price.
Thank You

You might also like