SlideShare a Scribd company logo
1 of 19
Arithmetic Operators
Topics
 Arithmetic Operators
 Operator Precedence
 Evaluating Arithmetic Expressions
 In-class Project
 Incremental Programming
Reading
 Section 2.5
Arithmetic Operators in C
Name Operator Example
Addition + num1 + num2
Subtraction - initial - spent
Multiplication * fathoms * 6
Division / sum / count
Modulus % m % n
Division
 If both operands of a division expression
are integers, you will get an integer
answer. The fractional portion is thrown
away.
 Examples : 17 / 5 = 3
4 / 3 = 1
35 / 9 = 3
Division (con’t)
 Division where at least one operand is a
floating point number will produce a
floating point answer.
 Examples : 17.0 / 5 = 3.4
4 / 3.2 = 1.25
35.2 / 9.1 = 3.86813
 What happens? The integer operand is
temporarily converted to a floating point,
then the division is performed.
Division By Zero
 Division by zero is mathematically
undefined.
 If you allow division by zero in a
program, it will cause a fatal error.
Your program will terminate
execution and give an error
message.
 Non-fatal errors do not cause
program termination, just produce
incorrect results.
Modulus
 The expression m % n yields the integer
remainder after m is divided by n.
 Modulus is an integer operation -- both operands
MUST be integers.
 Examples : 17 % 5 = 2
6 % 3 = 0
9 % 2 = 1
5 % 8 = 5
 Used to determine if an integer value is even or
odd
5 % 2 = 1 odd 4 % 2 = 0 even
If you take the modulus by 2 of an integer, a
result of 1 means the number is odd and a result
of 0 means the number is even.
 The Euclid’s GCD Algorithm (done earlier)
Uses for Modulus
Arithmetic Operators
Rules of Operator Precedence
Operator(s) Precedence & Associativity
( )
Evaluated first. If nested (embedded),
innermost first. If on same level, left to right.
* / %
Evaluated second. If there are several,
evaluated left to right
+ -
Evaluated third. If there are several, evaluated
left to right.
= Evaluated last, right to left.
Using Parentheses
 Use parentheses to change the order in which an
expression is evaluated.
 a + b * c Would multiply b * c first, then add a to
the result.
 If you really want the sum of a and b to be
multiplied by c, use parentheses to force the
evaluation to be done in the order you want.
(a + b) * c
 Also use parentheses to clarify a complex
expression.
Practice With Evaluating Expressions
Given integer variables a, b, c, d, and e,
where a = 1, b = 2, c = 3, d = 4,
evaluate the following expressions:
a + b - c + d
a * b / c
1 + a * b % c
a + d % b - c
e = b = d + c / b - a
 Let’s write a program that computes and
displays the volume and surface area of a cube.
 Procedure:
 Use the pseudocode that we developed in
“Algorithms, Part 3 of 3”
 Convert the algorithm to code
 Clean up the code (spacing, indentation,
commenting)
A Sample Project
The Box - Pseudocode
Display “Enter the height: “
Read <height>
While (<height> <= 0 )
Display “The height must be > 0”
Display “Enter the height: “
Read <height>
End_while
The Box - Pseudocode (con’t)
Display “Enter the width: “
Read <width>
While (<width> <= 0 )
Display “The width must be > 0”
Display “Enter the width: “
Read <width>
End_while
The Box - Pseudocode (con’t)
Display “Enter the depth: “
Read <depth>
While (<depth> <= 0 )
Display “The depth must be > 0”
Display “Enter the depth: “
Read <depth>
End_while
The Box - Pseudocode (con’t)
<volume> = <height> X <width> X <depth>
<surface1> = <height> X <width>
<surface2> = <width> X <depth>
<surface3> = <height> X <depth>
<surface area> = 2 X (<surface1> + <surface2> + <surface3>)
The Box - Pseudocode (con’t)
Display “Height = “, <height>
Display “Width = “, <width>
Display “Depth = “, <depth>
Display “Volume = “, <volume>
Display “Surface Area = “, <surface area>
Good Programming Practice
 It is best not to take the “big bang” approach to
coding.
 Use an incremental approach by writing your
code in incomplete, yet working, pieces.
 For example, for your projects,
 Don’t write the whole program at once.
 Just write enough to display the user prompt on
the screen.
 Get that part working first (compile and run).
 Next, write the part that gets the value from the
user, and then just print it out.
Always have a working
version of your program!
Good Programming Practice
 Get that working (compile and run).
 Next, change the code so that you use the
value in a calculation and print out the answer.
 Get that working (compile and run).
 Continue this process until you have the final
version.
 Get the final version working.
Using the Incremental Approach
 Let’s think about how we could have developed
the volume and surface area program
incrementally.

More Related Content

Similar to C Programing Arithmetic Operators.ppt

OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxgerardkortney
 
Rightand wrong[1]
Rightand wrong[1]Rightand wrong[1]
Rightand wrong[1]slange
 
Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3Paul Brebner
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxSALU18
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1rajnidhiman
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Alpro
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schoolsDan Bowen
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and SelectionAhmed Nobi
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteachingeteaching
 

Similar to C Programing Arithmetic Operators.ppt (20)

Pseudocode By ZAK
Pseudocode By ZAKPseudocode By ZAK
Pseudocode By ZAK
 
OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docx
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
Rightand wrong[1]
Rightand wrong[1]Rightand wrong[1]
Rightand wrong[1]
 
Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1
 
Algorithmsandflowcharts1
Algorithmsandflowcharts1Algorithmsandflowcharts1
Algorithmsandflowcharts1
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
Computer paper 3 may june 2004 9691 cambridge General Certificate of educatio...
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
C-PPT.pdf
C-PPT.pdfC-PPT.pdf
C-PPT.pdf
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 
additional.pptx
additional.pptxadditional.pptx
additional.pptx
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

C Programing Arithmetic Operators.ppt

  • 1. Arithmetic Operators Topics  Arithmetic Operators  Operator Precedence  Evaluating Arithmetic Expressions  In-class Project  Incremental Programming Reading  Section 2.5
  • 2. Arithmetic Operators in C Name Operator Example Addition + num1 + num2 Subtraction - initial - spent Multiplication * fathoms * 6 Division / sum / count Modulus % m % n
  • 3. Division  If both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away.  Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 3
  • 4. Division (con’t)  Division where at least one operand is a floating point number will produce a floating point answer.  Examples : 17.0 / 5 = 3.4 4 / 3.2 = 1.25 35.2 / 9.1 = 3.86813  What happens? The integer operand is temporarily converted to a floating point, then the division is performed.
  • 5. Division By Zero  Division by zero is mathematically undefined.  If you allow division by zero in a program, it will cause a fatal error. Your program will terminate execution and give an error message.  Non-fatal errors do not cause program termination, just produce incorrect results.
  • 6. Modulus  The expression m % n yields the integer remainder after m is divided by n.  Modulus is an integer operation -- both operands MUST be integers.  Examples : 17 % 5 = 2 6 % 3 = 0 9 % 2 = 1 5 % 8 = 5
  • 7.  Used to determine if an integer value is even or odd 5 % 2 = 1 odd 4 % 2 = 0 even If you take the modulus by 2 of an integer, a result of 1 means the number is odd and a result of 0 means the number is even.  The Euclid’s GCD Algorithm (done earlier) Uses for Modulus
  • 8. Arithmetic Operators Rules of Operator Precedence Operator(s) Precedence & Associativity ( ) Evaluated first. If nested (embedded), innermost first. If on same level, left to right. * / % Evaluated second. If there are several, evaluated left to right + - Evaluated third. If there are several, evaluated left to right. = Evaluated last, right to left.
  • 9. Using Parentheses  Use parentheses to change the order in which an expression is evaluated.  a + b * c Would multiply b * c first, then add a to the result.  If you really want the sum of a and b to be multiplied by c, use parentheses to force the evaluation to be done in the order you want. (a + b) * c  Also use parentheses to clarify a complex expression.
  • 10. Practice With Evaluating Expressions Given integer variables a, b, c, d, and e, where a = 1, b = 2, c = 3, d = 4, evaluate the following expressions: a + b - c + d a * b / c 1 + a * b % c a + d % b - c e = b = d + c / b - a
  • 11.  Let’s write a program that computes and displays the volume and surface area of a cube.  Procedure:  Use the pseudocode that we developed in “Algorithms, Part 3 of 3”  Convert the algorithm to code  Clean up the code (spacing, indentation, commenting) A Sample Project
  • 12. The Box - Pseudocode Display “Enter the height: “ Read <height> While (<height> <= 0 ) Display “The height must be > 0” Display “Enter the height: “ Read <height> End_while
  • 13. The Box - Pseudocode (con’t) Display “Enter the width: “ Read <width> While (<width> <= 0 ) Display “The width must be > 0” Display “Enter the width: “ Read <width> End_while
  • 14. The Box - Pseudocode (con’t) Display “Enter the depth: “ Read <depth> While (<depth> <= 0 ) Display “The depth must be > 0” Display “Enter the depth: “ Read <depth> End_while
  • 15. The Box - Pseudocode (con’t) <volume> = <height> X <width> X <depth> <surface1> = <height> X <width> <surface2> = <width> X <depth> <surface3> = <height> X <depth> <surface area> = 2 X (<surface1> + <surface2> + <surface3>)
  • 16. The Box - Pseudocode (con’t) Display “Height = “, <height> Display “Width = “, <width> Display “Depth = “, <depth> Display “Volume = “, <volume> Display “Surface Area = “, <surface area>
  • 17. Good Programming Practice  It is best not to take the “big bang” approach to coding.  Use an incremental approach by writing your code in incomplete, yet working, pieces.  For example, for your projects,  Don’t write the whole program at once.  Just write enough to display the user prompt on the screen.  Get that part working first (compile and run).  Next, write the part that gets the value from the user, and then just print it out.
  • 18. Always have a working version of your program! Good Programming Practice  Get that working (compile and run).  Next, change the code so that you use the value in a calculation and print out the answer.  Get that working (compile and run).  Continue this process until you have the final version.  Get the final version working.
  • 19. Using the Incremental Approach  Let’s think about how we could have developed the volume and surface area program incrementally.