SlideShare a Scribd company logo
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

More Related Content

PPSX
CS106 Lab 8 - Nested loops
PPSX
CS106 Lab 7 - For loop
PPSX
CS106 Lab 11 - Functions (passing by reference)
PPSX
CS106 Lab 10 - Functions (passing by value)
PPSX
CS106 Lab 6 - While and Do..While loop
PPSX
CS106 Lab 4 - If statement
PPSX
CS106 Lab 9 - 1D array
PPSX
CS106 Lab 5 - Switch case
CS106 Lab 8 - Nested loops
CS106 Lab 7 - For loop
CS106 Lab 11 - Functions (passing by reference)
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 6 - While and Do..While loop
CS106 Lab 4 - If statement
CS106 Lab 9 - 1D array
CS106 Lab 5 - Switch case

What's hot (20)

PDF
Distributed Radar Tracking Simulation Project
PPT
Code Tuning
PPT
Concept of c
PPT
Matlab Nn Intro
PPT
Analysis of algo
PPTX
Your first neural network model
PDF
Intelligent Digit Recognition Engine: Architecture
PDF
Efficiently Performing Arithmetic Operation over Fq18 Extension Field
PDF
Hw5 2017-spring
PPTX
Insertion sort
PPTX
Lecture4
PDF
Code Optimizatoion
PPT
Model and Design
PPTX
Functions in c++
DOCX
applet.docx
PDF
Linear Convolution using Matlab Code
PDF
Svd filtered temporal usage clustering
PPT
Stack a Data Structure
PDF
Insertion sort
PDF
Anders Nielsen template model-builder
Distributed Radar Tracking Simulation Project
Code Tuning
Concept of c
Matlab Nn Intro
Analysis of algo
Your first neural network model
Intelligent Digit Recognition Engine: Architecture
Efficiently Performing Arithmetic Operation over Fq18 Extension Field
Hw5 2017-spring
Insertion sort
Lecture4
Code Optimizatoion
Model and Design
Functions in c++
applet.docx
Linear Convolution using Matlab Code
Svd filtered temporal usage clustering
Stack a Data Structure
Insertion sort
Anders Nielsen template model-builder
Ad

Similar to CS106 Lab 3 - Modulus (20)

PPTX
PDF
Introduction to computing Processing and performance.pdf
PPTX
CPP Homework Help
PDF
An Introduction to MATLAB with Worked Examples
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
PDF
complexity analysis.pdf
PPT
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...
PPTX
Matlab Functions for programming fundamentals
PDF
Complex models in ecology: challenges and solutions
PDF
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
PPTX
1.1Introduction to matlab.pptx
PPT
chapter 1
DOCX
Ecs 10 programming assignment 4 loopapalooza
PDF
Dsp lab _eec-652__vi_sem_18012013
PDF
Dsp lab _eec-652__vi_sem_18012013
DOCX
B61301007 matlab documentation
PPTX
Programming python quick intro for schools
PPTX
Mat lab workshop
PPT
AA Lecture 01 of my lecture os ghhhggh.ppt
PPT
cpphtp4_PPT_03.ppt
Introduction to computing Processing and performance.pdf
CPP Homework Help
An Introduction to MATLAB with Worked Examples
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
complexity analysis.pdf
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...
Matlab Functions for programming fundamentals
Complex models in ecology: challenges and solutions
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
1.1Introduction to matlab.pptx
chapter 1
Ecs 10 programming assignment 4 loopapalooza
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
B61301007 matlab documentation
Programming python quick intro for schools
Mat lab workshop
AA Lecture 01 of my lecture os ghhhggh.ppt
cpphtp4_PPT_03.ppt
Ad

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
master seminar digital applications in india
PPTX
Presentation on HIE in infants and its manifestations
PDF
Classroom Observation Tools for Teachers
PDF
RMMM.pdf make it easy to upload and study
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Pharma ospi slides which help in ospi learning
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
A systematic review of self-coping strategies used by university students to ...
O5-L3 Freight Transport Ops (International) V1.pdf
master seminar digital applications in india
Presentation on HIE in infants and its manifestations
Classroom Observation Tools for Teachers
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Institutional Correction lecture only . . .
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharmacology of Heart Failure /Pharmacotherapy of CHF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3

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
  • 4. Arithmetic Operators Operator Description + Addition - Subtraction * Multiplication / Division % Modulo Presented by TA. Nada Kamel
  • 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
  • 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.
  • 12. <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)
  • 13. <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
  • 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
  • 17. 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
  • 18. Presented by TA. Nada Kamel
  • 20. 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
  • 21. Presented by TA. Nada Kamel
  • 22. 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
  • 23. Presented by TA. Nada Kamel
  • 24. 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
  • 25. Presented by TA. Nada Kamel
  • 26. Any Questions? Presented by TA. Nada Kamel