04 - Basic Math Operations

There are seven basic math operators that you can apply to your code to do simple math calculations.

Here is an example of the math operators being used. Note that the operations are done on the right side of the equal sign.

Activity 1

Write a program that uses all 4 different operations.

Make two of the results of the operations come out as integer types, and the other 2 results come out as a double type.

Print the resulting variables using complete sentences

The mod/modulus/modulo operator (%) returns the remainder when 2 numbers are divided. Below is an example of 8%3. The remainder is 2.

Activity 2

Add to the code above to find the mod of following.

    • 7%5

    • 34%3

    • 4%2

    • 2%6

    • 4%6

Importing the Math header

The "math.h" header defines many math functions for you. Below are a few common functions we will use. Here is a complete list - Link

All of the math functions in math.h take in double-type numbers as inputs/arguments and return double-type numbers as outputs.

Below is example of these being used:

Activity 3

Import the math header.

Write a program that declares two double-type variables and sets their values.

Call on three separate math functions from the math header.

Print the original variable values as well as the resulting values after the math functions in complete sentences.