Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary.

Slides:



Advertisements
Similar presentations
Evaluate expressions with grouping symbols
Advertisements

Class 7.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 Objectives Understand streamed input and output.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
CS150 Introduction to Computer Science 1
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Starting Out with C++, 3 rd Edition 1 Chapter 2. Introduction to C++
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Taks Objective 2 Properties and attributes of function.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CSC 107 – Programming For Science. The Week’s Goal.
Arithmetic in Pascal A Short Glance We will learn the followings in this chapter Arithmetic operators Order of precedence Assignment statements Arithmetic.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1-2 Order of Operations and Evaluating Expressions.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Write as an Algebraic Expression The product of a number and 6 added to a.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Addition Multiplication Subtraction Division. 1.If the signs are the same, add the numbers and keep the same sign = = If the.
1 CS161 Introduction to Computer Science Topic #6.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1-2 Order of Operations Objective: Use the order of operations to evaluate expressions.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
1 Chapter 2 Introduction to C++. 2 Topics 2.1 Parts of a C++ Program 2.2 The cout Object 2.3 The #include Directive 2.4 Variables and Constants 2.5 Identifiers.
Chapter 2. Introduction to C++
WARM UP Page 9 “Check Skills You’ll Need” # 1 – 12.
Chapter 3. Expressions and Interactivity
Assignment statement:
Arithmetic operations & assignment statement
Arithmetic & other operations
Introduction to C++ October 2, 2017.
Operators and Expressions
Algebraic expression to Verbal statement Basic
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Algebraic expression to verbal statement two terms
Multiply and divide Expressions
Algebra with Whole Numbers
Presentation transcript:

Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary

Arithmetic Expressions Algebraic Exp. C++ Expression f + 7 p - c bx r mod s f + 7 p - c b * x x / y r % s x/y or x y

RegWages ?.? OTWages ?.? OTHours 10 RegHours 40.0 OTPay BasePay TotalWages ?.? #include #include void main () { double RegWages, BasePay = 18.25; double RegHours = 40.0; double RegHours = 40.0; double OTWages, OTPay = 27.78; double OTWages, OTPay = 27.78; double OTHours = 10; double OTHours = 10; double TotalWages; double TotalWages;

Basic usage of operators RegWages ?.? OTWages ?.? OTHours 10 RegHours 40.0 OTPay BasePay TotalWages ?.? RegWages = BasePay * RegHours; RegWages = BasePay * RegHours; OTWages = OTPay * OTHours; OTWages = OTPay * OTHours; TotalWages = RegWages + OTWages; TotalWages = RegWages + OTWages; cout << “Wages for this week are $ ” cout << “Wages for this week are $ ” << TotalWages ; << TotalWages ; } 730.0

Precedence of Operations Operator (s) Operation(s) ( ) *, /, or % + or - Parentheses Multiplication Division Modulus Addition Subtraction

How precedence works x = ( ) / 3 * % 3 x = ? / 3 x =3* 2 * % % 3 x =+ 5 % 36 x =

Compound Operators += - =- =- =- = *= %= x = x + 5; y = y - 2; z = z * 10; a = a / b c = c % 3; /= x += 5; y - = 2; z *= 10; c %= 3; a /= b;

The Typecast Operator #Include void main () {int Months, Books; double PerMonth; cout << “How many books do” <<“you plan to read?”; cin >> Books; cout << “ How many months will” <<“it take you to read them?”; cin >> Months; PerMonth = static_cast (Books) / Months; cout << “That is” << PerMonth” <<“books per month.\n; }

The Typecast Operator #Include void main () {int Months, Books; double PerMonth; cout << “How many books do” << “you plan to read?”; cin >> Books; cout << “ How many months will” << “it take you to read them?”; cin >> Months; PerMonth = static_cast (Books) / Months; cout << “That is” << PerMonth << “books per month.\n”; }

The Typecast Operator #include void main () { int Number = 65; cout << Number << endl; // C method of type casting cout << char(Number) << endl; }

cout << (a + 5); Expressions with a value

Statement Screen Output cout << (a + b);11 cout << (b * 2);14 cout << (b + (a*2));15 cout << (a = 6);6 Expressions with a value Assume a is 4 and b is 7

Duo ? 5 10 Unus ? 59 Tres ? _ 11 _ 1 _ # include void main () { int Unus, Duo, Tres; Unus = Duo = Tres = 5; Unus = Duo = Tres = 5; Unus += 4; Unus += 4; Duo *= 2; Duo *= 2; Tres -= 4; Tres -= 4; Unus /= 3; Unus /= 3; Duo += Tres; Duo += Tres; cout << Unus << endl; cout << Unus << endl; cout << Duo << endl; cout << Duo << endl; cout << Tres << endl; cout << Tres << endl; }

Unusual ? Strange ? Mystery ? _ 5 _ 10 _ # include # include void main () void main () { int Unusual, Mystery, Strange; cout << (Unusual = 2) << endl; cout << (Unusual = 2) << endl; cout << (Strange = Unusual + 3) << endl; cout << (Strange = Unusual + 3) << endl; cout << (Mystery = Strange * 2) cout << (Mystery = Strange * 2) << endl; << endl;

Unusual ? Strange ? Mystery ? _ 5 _ 10 _ 2 _ 5 _ 10 _ cout << Unusual << endl; cout << Unusual << endl; cout << Strange << endl; cout << Strange << endl; cout << Mystery << endl; cout << Mystery << endl; }

What is this program’s output #include #include void main () { int X = 0, Y = 2; X = Y * 4; X = Y * 4; cout << X << endl << Y << endl; cout << X << endl << Y << endl; } 8 2 _

Write assignment statements A) Adds 2 to A and stores the result in B. B = A + 2

Write assignment statements B) Multiplies B times 4 and stores the result in A. A = B * 4;

Write assignment statements C) Divides A by 3.14 and stores the result in B. B = A / 3.14;

Write assignment statements D) Subtracts 8 from B and stores the result in A. A = B - 8;

What is this program’s output void main () { int A, X = 23; int A, X = 23; A = X % 2; A = X % 2; cout << X << endl << A << endl; cout << X << endl << A << endl; } 23 1 _