Lab 3
CS106 - Fundamentals of Computer Science
Presented by TA. Nada Kamel
Agenda
 Arithmetic Operators
 C++ Operator Precedence
 Modulus
 <cmath> library
 <iomanip> library
 Example
 Hands-on problems
Presented by TA. Nada Kamel
Arithmetic Operators
Presented by TA. Nada Kamel
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
Presented by TA. Nada Kamel
Operators Precedence
Presented by TA. Nada Kamel
Priority of operators
1. () (brackets)
2. * , /, % (Multiplication, Division, Modulus)
3. + , - (Addition, Subtraction)
4. = (Assignment operator)
Examples:
int x = 3 * 5 / 2;
// x = 7
int x = 3 * (5 / 2);
// x = 6
Presented by TA. Nada Kamel
Priority of operators (Cont.)
The most important rule when assigning is the right-to-left rule..
The assignment operation always takes place from right to left, and never the
other way.
Example
int a;
int x = 7;
int y = 3;
a = x + y;
Presented by TA. Nada Kamel
Modulus
Presented by TA. Nada Kamel
Modulus
Presented by TA. Nada Kamel
 It is represented by a percentage sign (%), gives the remainder of a
division of two values.
 Example:
int remainder = 11 % 3;
The value stored in variable remainder will be equal to 2, since dividing 11 by
3 results in 3, with a remainder of 2.
Modulus (Cont.)
Presented by TA. Nada Kamel
Libraries
Presented by TA. Nada Kamel
<cmath> library
 It contains functions to compute common mathematical operations and
transformations:
 Functions like power, square root, cosine, sine, tangent, etc.
Presented by TA. Nada Kamel
Function In C++
2 to the power of 6 ( 26
) pow(2, 6)
Square root of 15 sqrt(15)
cosine of 60 cos(60 * PI / 180)
sine of 30 sin(30 * PI / 180)
<iomanip> library
 It contains many functions to manipulate in output of parameters.
 One of the functions we will use in this course is
setprecision
 Sets the decimal precision to be used to format floating-point values on
output operations.
 Example:
cout << fixed << setprecision(3) << 12.45657 << endl;
Presented by TA. Nada Kamel
Presented by TA. Nada Kamel
<iomanip> library (Cont.)
 Another function that we are going to use is
setw which sets field width.
Presented by TA. Nada Kamel
Example on setprecision
Presented by TA. Nada Kamel
Example
Write a program to calculate the average of 3 numbers entered by the user.
Round the average to 2 decimal places.
Example 1:
Input: 30.9 45.73 60
Output : "The average equals 45.5433 and is rounded to 45.54"
Example 2:
Input: 10.6759 67.892 42.86
Output: "The average equals 40.476 and is rounded to 40.48"
Presented by TA. Nada Kamel
Presented by TA. Nada Kamel
Hands-on Problems
Presented by TA. Nada Kamel
Problem 1
You take a bag of mini chocolate bars to the camp. You are going to share them
with the guys in your tent.
There are 7 guys in the tent and 100 chocolate bars.
Write a C++ program to compute
 How many can each person have if you want everyone to have the same
amount?
 How many chocolate bars will be left over?
Presented by TA. Nada Kamel
Presented by TA. Nada Kamel
Problem 2
Write a C++ program that reads a large number of seconds and displays it in
terms of an integer number of hours, an integer number of minutes, and
remaining seconds (if any).
Examples:
Input Sample: Output Sample:
1432 00:23:52
Input Sample: Output Sample:
5382 01:29:42
Presented by TA. Nada Kamel
Presented by TA. Nada Kamel
Problem 3
Write a C++ program to enter an integer number of 4 digits and display it in
an inverse order.
Example:
Input Sample:
Enter 4-digit number:
5723
Output Sample:
Inverse of the number is 3275
Presented by TA. Nada Kamel
Presented by TA. Nada Kamel
Any Questions?
Presented by TA. Nada Kamel

CS106 Lab 3 - Modulus

  • 1.
    Lab 3 CS106 -Fundamentals of Computer Science Presented by TA. Nada Kamel
  • 2.
    Agenda  Arithmetic Operators C++ Operator Precedence  Modulus  <cmath> library  <iomanip> library  Example  Hands-on problems Presented by TA. Nada Kamel
  • 3.
  • 4.
    Arithmetic Operators Operator Description +Addition - Subtraction * Multiplication / Division % Modulo Presented by TA. Nada Kamel
  • 5.
  • 6.
    Priority of operators 1.() (brackets) 2. * , /, % (Multiplication, Division, Modulus) 3. + , - (Addition, Subtraction) 4. = (Assignment operator) Examples: int x = 3 * 5 / 2; // x = 7 int x = 3 * (5 / 2); // x = 6 Presented by TA. Nada Kamel
  • 7.
    Priority of operators(Cont.) The most important rule when assigning is the right-to-left rule.. The assignment operation always takes place from right to left, and never the other way. Example int a; int x = 7; int y = 3; a = x + y; Presented by TA. Nada Kamel
  • 8.
  • 9.
    Modulus Presented by TA.Nada Kamel  It is represented by a percentage sign (%), gives the remainder of a division of two values.  Example: int remainder = 11 % 3; The value stored in variable remainder will be equal to 2, since dividing 11 by 3 results in 3, with a remainder of 2.
  • 10.
  • 11.
  • 12.
    <cmath> library  Itcontains functions to compute common mathematical operations and transformations:  Functions like power, square root, cosine, sine, tangent, etc. Presented by TA. Nada Kamel Function In C++ 2 to the power of 6 ( 26 ) pow(2, 6) Square root of 15 sqrt(15) cosine of 60 cos(60 * PI / 180) sine of 30 sin(30 * PI / 180)
  • 13.
    <iomanip> library  Itcontains many functions to manipulate in output of parameters.  One of the functions we will use in this course is setprecision  Sets the decimal precision to be used to format floating-point values on output operations.  Example: cout << fixed << setprecision(3) << 12.45657 << endl; Presented by TA. Nada Kamel
  • 14.
    Presented by TA.Nada Kamel
  • 15.
    <iomanip> library (Cont.) Another function that we are going to use is setw which sets field width. Presented by TA. Nada Kamel
  • 16.
  • 17.
    Example Write a programto calculate the average of 3 numbers entered by the user. Round the average to 2 decimal places. Example 1: Input: 30.9 45.73 60 Output : "The average equals 45.5433 and is rounded to 45.54" Example 2: Input: 10.6759 67.892 42.86 Output: "The average equals 40.476 and is rounded to 40.48" Presented by TA. Nada Kamel
  • 18.
    Presented by TA.Nada Kamel
  • 19.
  • 20.
    Problem 1 You takea bag of mini chocolate bars to the camp. You are going to share them with the guys in your tent. There are 7 guys in the tent and 100 chocolate bars. Write a C++ program to compute  How many can each person have if you want everyone to have the same amount?  How many chocolate bars will be left over? Presented by TA. Nada Kamel
  • 21.
    Presented by TA.Nada Kamel
  • 22.
    Problem 2 Write aC++ program that reads a large number of seconds and displays it in terms of an integer number of hours, an integer number of minutes, and remaining seconds (if any). Examples: Input Sample: Output Sample: 1432 00:23:52 Input Sample: Output Sample: 5382 01:29:42 Presented by TA. Nada Kamel
  • 23.
    Presented by TA.Nada Kamel
  • 24.
    Problem 3 Write aC++ program to enter an integer number of 4 digits and display it in an inverse order. Example: Input Sample: Enter 4-digit number: 5723 Output Sample: Inverse of the number is 3275 Presented by TA. Nada Kamel
  • 25.
    Presented by TA.Nada Kamel
  • 26.