SlideShare a Scribd company logo
FP 201 – Programming
Fundamentals
Operators & Expressions
Learning outcome
By the end of the course, students should be able to:
Define the following operators:
        a. Arithmetic operator
        b. Assignment operator
        c. Increment and decrement operator
        d. Relational operator
        e. Logical operator
Explain operators’ precedence
Explain type conversion
Write expression using operator
Use expression in program
Operators
   Operator                    Action
      -       subtraction (also unary minus)
     +        addition
      *       multiplication
      /       division
     %        modulus division
      --      decrement
     ++       increment
     &&       AND
     ||       OR
      !       NOT
Operators
Is a symbol that instructs compiler to perform an operation
  or action.
Arithmetic Operators
  Perform four fundamental operations which are addition,
        subtraction, multiplication and division.
Operator     Description                          Example   Result
   *         Multiplication                        2*8       16
   /         Division                              8/2        4
   +         Addition                              2+8       10
   -         Subtraction                            8-2       6
   ()        Parentheses. For grouping             (8+2)     10
   %         Modulus. Divides two number and       4%3        1
             returns just the remainder portion
Arithmetic Operators
arithmetic expressions are evaluated with some rule called
  operator precedence.
  multiplications (*) and divisions (/) are evaluated first
  additions (+) and subtractions (-) are performed last.
Activity 1

Write the resultant value of the following expressions.


       Expression             Result
       14-4
       14+4
       14* 4
       14/4
       14%4
ACTIVITY 2
     Calculate the total marks for the given five subjects.
     Declare appropriate variables, assign values as given
     below:
       English = 85
       Maths = 100
       History = 75
       Geography = 70
       Art = 85

         calculate the total marks
         calculate the average
#include<iostream>

using namespace std;
int main()
{
   int english = 85, maths = 100, history = 75, geography = 70, art = 85;
   int sum=0;

    sum=english+maths+history+geography+art;
    cout<<"Sum is: "<<sum<<endl;;

    return 0;
}
#include<iostream>

using namespace std;
int main()
{
    int english = 85, maths = 100, history = 75, geography = 70, art = 85;
    int sum=0, average=0;

    sum=english+maths+history+geography+art;
    average=sum/5;

    cout<<"Sum is: "<<sum<<endl;
    cout<<"Average is:"<<average<<endl;

    return 0;
}
Assignment Operator
variables are given a value through the use of assignment
 operator (=)
general form:
 variable = expression;
eg:
  int   x, y;
  x =   2;
  y =   5*x; // y = 10
  x =   x + 4; // x = 6
  y =   y/2; // y = 5
Assignment Operator
The value of an assignment can be used in another
  assignment.

This is a chained assignment.
Eg:
    m = (n = 66) + 9; // n = 66 and m = 75
    x = y = 22; // x = 22 and y = 22
Compound Assignment Operators
(+=), (-=), (*=), (/=), (%=)


allow us to use assignment and an arithmetic operator
  together.

general form is:
  variable operator= expression;
Compound Assignment Operators
Operator         Description          Example   Equivalent to

  +=            add and assign        x += 3     x=x+3

  -=         subtract and assign      x -= 5      x=x-5

  *=         multiply and assign      x *= 4     x=x*4

  /=          divide and assign       x /= 2     x=x/2



  %=       find reminder and assign   x %= 9     x=x%9
In Class Exercise
 Given the following declaration and initial assignment:


   int i = 4, j = 21, k;
   float x = 94.55, y;
   char a = ‘z’, b = ‘t’, c =‘ ‘;

   Determine the value for the following assignments:
    k=i*j
   y = x + i
   y = k = j
    a = b =c
    i += i
Increment & Decrement Operator
increment operator (++) and decrement operator (--)
 increase or reduce by one the value stored in a variable.
the following are equivalent in functionality.
  x++;
  x += 1;
  x = x + 1;
characteristic of this operator:
  can be used both as a prefix and as a postfix.
  eg: a++ or ++a
  the results is different
Prefix
When a prefix expression (++x or --x) is used as part
  of an expression, the value returned is the value
  calculated after the prefix operator is applied

  int x = 0;
  int y = 0;
  y = ++x;
  // result: y=1, x=1 x is incremented by 1 and the result is
  assigned to y
Postfix
When a postfix expression (x++ or x--) is used as part
  of an expression, the value returned is the value
  calculated before the postfix operator is applied

  int x = 0;
  int y = 0;
  y = x++;
  // result: y=0, x=1 original value of x is stored, x is
  incremented, original value of x is assigned to y
Increment & Decrement Operator
eg:
 a = 5; // a = 5
 b = a++; // b = a = 5, then a = 5+1 = 6
 c = ++a; // a = 6+1 = 7, then c = a = 7
Relational Operator
    Control statements use relation operators in order to compare two objects,
     which results in a value that is either true (1) or false (0).
    There are six relational operators as follows:



Operator            Description                         Example            Value
   <                   less than               x<y                6<9        1

  <=             less than or equal to        x <= y           12 <= 13      1

   >                 greater than              x>y                7 > 10     0

  >=           greater than or equal to       x >= y              9 >= 5     1

  ==                   equal to               x == y              7 ==5      0

   !=                not equal to              x != y             6 != 5     1
Logical Operator
Operator   Description                                    Example

           logical AND, conjunction.
  &&       Both sides must be true for the result to   x > 2 && y == 3
           be true


           logical OR, disjunction.
  ||       The result is true if either side or both   x > 2 || x <= 9
           sides are true.


   !       logical NOT, negation                           !(x>0)
Condition1   Condition2   &&       ||

   true         true      true    true

   true        false      false   true

  false         true      false   true

  false        false      false   false
Logical Operator
using these operators, we can form a Boolean Expression that
 can be either true or false.
Boolean expressions evaluate to integer values.
The value 0 means 'false' and all non-zero value means 'true'.
There are also two literal constants used with bool
 variables: true and false.
In Class Exercise
What is the difference between x = 3 and x == 3?
Given a = 2, b = 5, c = 7 determine the value for the
  following expressions either TRUE or FALSE: :
   1.   a < 2 && b > 5
   2.   c >= a
   3.   a < b || b > c
   4.   c != 10
   5.   a == 25
Operator Precedence
  Determines the order in which mathematical computations
    and operations are performed in an expression.
Operator Precedence
Operator Precedence

Given a=b = 5, c = 2; solve the following expression
 a+b*2/c–5
 =5+5*2/2–5
 = 5 + 10 / 2 – 5
 =5+5–5
 = 10 – 5
 =5
In Class Exercise
Given x = 10, y = 7, z = 2; solve the following expression
  x + 2y – z
  x / z – x * x + y
  x * y % z
  5 ( x + y + z ) – x / z
  ++x*y – y*z
Type conversion/Type Casting
Is the conversion of a value of one data type into another
 data type.
Typecasting consist of two types:
     Implicit conversion
     Explicit conversion
Implicit conversion
A value of data type with lower range is assigned to a variable
  of data type with higher range
Lower range to higher range
Example
#include<iostream>
using namespace std;
                                 OUTPUT
void main()                      Result: 4.2555
{
  double x; // Size is 8 bytes
  int y=2; //Size is 4 bytes
  float z=2.2555f;
  x=y+z; //Implicit conversion
  cout<<"Result: "<<x;
}
Explicit conversion
A value of data type with higher range is assigned to a
  variable of data type with lower range
Higher range to lower range

Explicit conversion leads to data loss.
Example
#include<iostream>

using namespace std;
                                    OUTPUT
void main()                         The total value is:300
{
  int total_value;
  double book1 = 300.46;
   total_value=(int)book1; // Explicit conversion
  cout<<"The total value is:"<<total_value;
}
Exercise
Write a program to declare a variable d of type double.
  Assign the value 102.5789 to d. Declare a float variable fl.
  Assign the value of d to fl by performing an explicit
  conversion and print the value on the screen.
Answer
#include<iostream>
using namespace std;
void main()            OUTPUT
{                      Value of the float variable fl = 102.579
        double d=102.5789;
        float fl;
        fl=(float)d; // Explicit conversion
        cout<<"Value of the float variable fl = "<<fl;
}
Summary
In this presentation, you learnt the following:
Operator is a symbol that instructs compiler to perform an
  operation or action.
Typecasting is the conversion of data type of the value of a
  variable into another.
There are two types of casting: implicit conversion and
  explicit conversion.
FP 201 – Programming
Fundamentals with C++
Input Output Statements
Learning outcome
By the end of the course, students should be able to:

Identify the syntax use for input and output
Write program that use the input and output statements
Input/Output Statements
The standard C++ library includes the header file
  iostream, where the standard input and output stream
  objects are declared.

  cout to output data to the screen and


  cin to input data from the keyboard.
Standard Output
standard output:
  need to use cout, followed by the insertion operator (<<)
  eg:
    cout << "Hello World!"; // Outputs "Hello World!"
                          on the screen
    cout << 1299; // Outputs the number 1299 on the
        screen
    cout << x; // Outputs the content of x on the
      screen
insertion operator (<<) may be used more than once in a
  single statement:
  cout << "Hi, " << "this is " << "a simple C++ statement";
Standard Output
Note that, cout does not add a line break.
For example:
  cout << "Computer Programming ";
  cout << "is important for engineers.";
  will be output as:
       Computer Programming is important for
  engineers.
Standard Output
A line break can be added by inserting a 'n' character or a
  using a endl manipulator.

  cout << "Computer Programming n";
  cout << "is important for engineers.";


  cout << "Computer Programming " << endl;
  cout << "is important for engineers.";


These two produce the following (same) output: Computer
  Programming
  is important for engineers.
Standard Input
 Handling the standard input is done by applying the
  overloaded operator of extraction (>>) on the cin stream
 Value can be input from the keyboard and assigned to a
  variable x as follows:
  cin >> x;
 and two variables can assigned as any of the follows:
  cin >> x;
  cin >> y;
  cin >> x >> y;
Code Example
// Calculates the sum of two integers
#include <iostream>
using namespace std;

int main()
  {
  int a, b, total;
  cout << "Enter two integers to be added: ";
  cin >> a >> b;
  total = a + b;
  cout << "The sum is " << total << endl;
  return 0;
 }
Code Example
// Calculates the area of a triangle
// given an input of the triangle base and height.
#include <iostream>

int main()
  {
  float base, height, area;
  std::cout << "Enter the base and height of the
                triangle: ";
  std::cin >> base >> height;
  area = 0.5*base*height;
  std::cout << "The area of the triangle is "
            << area << std::endl;
}
Q&A
Develop a program that can calculate the total and average of
  four numbers that is input by user.
#include <iostream>
using namespace std;

int main()
  {
  int a, b, c, d, total, average;
  cout << "Enter four integers to be added: ";
  cin >> a >> b>>c>>d;
  total = a + b + c + d;
  average = total/4;
  cout << "The sum is " << total << endl;
  cout<< "The average is "<< average << endl;

 return 0;
 }
Summary
In this presentation, you learnt the following:

cout to output data to the screen
cin to input data from the keyboard.
cout use insertion operator (<<)
cin use overloaded operator of extraction (>>)

More Related Content

PPT
FP 201 Unit 3
PPT
FP 201 - Unit 3 Part 2
PPT
FP 201 - Unit 6
PPT
Fp201 unit5 1
PPT
Fp201 unit4
PPTX
C++ Pointers
PPTX
Iterative control structures, looping, types of loops, loop working
DOCX
Python unit 3 and Unit 4
FP 201 Unit 3
FP 201 - Unit 3 Part 2
FP 201 - Unit 6
Fp201 unit5 1
Fp201 unit4
C++ Pointers
Iterative control structures, looping, types of loops, loop working
Python unit 3 and Unit 4

What's hot (20)

PDF
C aptitude scribd
PPT
An imperative study of c
PPT
C++ Language
PPTX
Getting started in c++
PDF
Python Unit 3 - Control Flow and Functions
PPT
Pointers
PPT
Unit 6 pointers
PPT
Constructor,destructors cpp
PPTX
Programming Fundamentals
PPT
C++ programming
PPT
Computer Programming- Lecture 6
PDF
7 functions
PPT
PPT
Computer Programming- Lecture 10
PDF
PPTX
Basic c++ programs
PPT
Lecture04
PPT
Computer Programming- Lecture 7
C aptitude scribd
An imperative study of c
C++ Language
Getting started in c++
Python Unit 3 - Control Flow and Functions
Pointers
Unit 6 pointers
Constructor,destructors cpp
Programming Fundamentals
C++ programming
Computer Programming- Lecture 6
7 functions
Computer Programming- Lecture 10
Basic c++ programs
Lecture04
Computer Programming- Lecture 7
Ad

Viewers also liked (7)

PPT
Fp201 unit1 1
PPT
FP 201 - Unit4 Part 2
PPT
FP 201 Unit 2 - Part 2
DOC
Unit 3
DOC
Unit 1
DOC
Unit 2
PPT
Fp201 unit2 1
Fp201 unit1 1
FP 201 - Unit4 Part 2
FP 201 Unit 2 - Part 2
Unit 3
Unit 1
Unit 2
Fp201 unit2 1
Ad

Similar to FP 201 Unit 2 - Part 3 (20)

PPTX
C++ Programming Basics.pptx
PPTX
Operators and expressions in C++
PPT
Operator & Expression in c++
PPT
Reportgroup4 111016004939-phpapp01
PPT
Fundamentals of Programming Chapter 5
PPTX
Programming presentation
PPT
Project in TLE
PPT
Report Group 4 Constants and Variables
PPT
Report Group 4 Constants and Variables(TLE)
PPTX
Cs1123 4 variables_constants
PPT
Operation and expression in c++
PPTX
Lecture - Operators in C++ (Book: Tony Gaddis).pptx
PDF
Unit ii chapter 1 operator and expressions in c
PDF
02 - Data Types and Expressions using C.pdf
PPT
Cprogrammingoperator
PPT
Basic c operators
PDF
C++ revision tour
PPT
chap-3-operators.ppt it has all maths problems as comp operation is concerned...
PDF
Constructor and destructors
C++ Programming Basics.pptx
Operators and expressions in C++
Operator & Expression in c++
Reportgroup4 111016004939-phpapp01
Fundamentals of Programming Chapter 5
Programming presentation
Project in TLE
Report Group 4 Constants and Variables
Report Group 4 Constants and Variables(TLE)
Cs1123 4 variables_constants
Operation and expression in c++
Lecture - Operators in C++ (Book: Tony Gaddis).pptx
Unit ii chapter 1 operator and expressions in c
02 - Data Types and Expressions using C.pdf
Cprogrammingoperator
Basic c operators
C++ revision tour
chap-3-operators.ppt it has all maths problems as comp operation is concerned...
Constructor and destructors

More from rohassanie (20)

DOC
Course outline FP202 - Dis 2012
PPT
FP 202 - Chapter 5
PPT
Chapter 3 part 2
PPT
Chapter 3 part 1
PPT
FP 202 Chapter 2 - Part 3
DOCX
Lab ex 1
PPT
Chapter 2 (Part 2)
DOC
Labsheet 7 FP 201
DOC
Labsheet 6 - FP 201
DOCX
Jadual Waktu Sesi JUN 2012
DOC
Labsheet 5
DOC
Labsheet 4
PPT
Chapter 2 part 1
DOC
Labsheet_3
DOCX
Labsheet2 stud
DOC
Labsheet1 stud
PPT
Chapter 1 part 3
PPT
Chapter 1 part 2
PPT
Chapter 1 part 1
DOC
Introduction to C++
Course outline FP202 - Dis 2012
FP 202 - Chapter 5
Chapter 3 part 2
Chapter 3 part 1
FP 202 Chapter 2 - Part 3
Lab ex 1
Chapter 2 (Part 2)
Labsheet 7 FP 201
Labsheet 6 - FP 201
Jadual Waktu Sesi JUN 2012
Labsheet 5
Labsheet 4
Chapter 2 part 1
Labsheet_3
Labsheet2 stud
Labsheet1 stud
Chapter 1 part 3
Chapter 1 part 2
Chapter 1 part 1
Introduction to C++

FP 201 Unit 2 - Part 3

  • 1. FP 201 – Programming Fundamentals Operators & Expressions
  • 2. Learning outcome By the end of the course, students should be able to: Define the following operators: a. Arithmetic operator b. Assignment operator c. Increment and decrement operator d. Relational operator e. Logical operator Explain operators’ precedence Explain type conversion Write expression using operator Use expression in program
  • 3. Operators Operator Action - subtraction (also unary minus) + addition * multiplication / division % modulus division -- decrement ++ increment && AND || OR ! NOT
  • 4. Operators Is a symbol that instructs compiler to perform an operation or action.
  • 5. Arithmetic Operators Perform four fundamental operations which are addition, subtraction, multiplication and division. Operator Description Example Result * Multiplication 2*8 16 / Division 8/2 4 + Addition 2+8 10 - Subtraction 8-2 6 () Parentheses. For grouping (8+2) 10 % Modulus. Divides two number and 4%3 1 returns just the remainder portion
  • 6. Arithmetic Operators arithmetic expressions are evaluated with some rule called operator precedence. multiplications (*) and divisions (/) are evaluated first additions (+) and subtractions (-) are performed last.
  • 7. Activity 1 Write the resultant value of the following expressions. Expression Result 14-4 14+4 14* 4 14/4 14%4
  • 8. ACTIVITY 2 Calculate the total marks for the given five subjects. Declare appropriate variables, assign values as given below: English = 85 Maths = 100 History = 75 Geography = 70 Art = 85  calculate the total marks  calculate the average
  • 9. #include<iostream> using namespace std; int main() { int english = 85, maths = 100, history = 75, geography = 70, art = 85; int sum=0; sum=english+maths+history+geography+art; cout<<"Sum is: "<<sum<<endl;; return 0; }
  • 10. #include<iostream> using namespace std; int main() { int english = 85, maths = 100, history = 75, geography = 70, art = 85; int sum=0, average=0; sum=english+maths+history+geography+art; average=sum/5; cout<<"Sum is: "<<sum<<endl; cout<<"Average is:"<<average<<endl; return 0; }
  • 11. Assignment Operator variables are given a value through the use of assignment operator (=) general form: variable = expression; eg: int x, y; x = 2; y = 5*x; // y = 10 x = x + 4; // x = 6 y = y/2; // y = 5
  • 12. Assignment Operator The value of an assignment can be used in another assignment. This is a chained assignment. Eg: m = (n = 66) + 9; // n = 66 and m = 75 x = y = 22; // x = 22 and y = 22
  • 13. Compound Assignment Operators (+=), (-=), (*=), (/=), (%=) allow us to use assignment and an arithmetic operator together. general form is: variable operator= expression;
  • 14. Compound Assignment Operators Operator Description Example Equivalent to += add and assign x += 3 x=x+3 -= subtract and assign x -= 5 x=x-5 *= multiply and assign x *= 4 x=x*4 /= divide and assign x /= 2 x=x/2 %= find reminder and assign x %= 9 x=x%9
  • 15. In Class Exercise  Given the following declaration and initial assignment: int i = 4, j = 21, k; float x = 94.55, y; char a = ‘z’, b = ‘t’, c =‘ ‘; Determine the value for the following assignments:  k=i*j y = x + i y = k = j  a = b =c  i += i
  • 16. Increment & Decrement Operator increment operator (++) and decrement operator (--) increase or reduce by one the value stored in a variable. the following are equivalent in functionality. x++; x += 1; x = x + 1; characteristic of this operator: can be used both as a prefix and as a postfix. eg: a++ or ++a the results is different
  • 17. Prefix When a prefix expression (++x or --x) is used as part of an expression, the value returned is the value calculated after the prefix operator is applied int x = 0; int y = 0; y = ++x; // result: y=1, x=1 x is incremented by 1 and the result is assigned to y
  • 18. Postfix When a postfix expression (x++ or x--) is used as part of an expression, the value returned is the value calculated before the postfix operator is applied int x = 0; int y = 0; y = x++; // result: y=0, x=1 original value of x is stored, x is incremented, original value of x is assigned to y
  • 19. Increment & Decrement Operator eg: a = 5; // a = 5 b = a++; // b = a = 5, then a = 5+1 = 6 c = ++a; // a = 6+1 = 7, then c = a = 7
  • 20. Relational Operator  Control statements use relation operators in order to compare two objects, which results in a value that is either true (1) or false (0).  There are six relational operators as follows: Operator Description Example Value < less than x<y 6<9 1 <= less than or equal to x <= y 12 <= 13 1 > greater than x>y 7 > 10 0 >= greater than or equal to x >= y 9 >= 5 1 == equal to x == y 7 ==5 0 != not equal to x != y 6 != 5 1
  • 21. Logical Operator Operator Description Example logical AND, conjunction. && Both sides must be true for the result to x > 2 && y == 3 be true logical OR, disjunction. || The result is true if either side or both x > 2 || x <= 9 sides are true. ! logical NOT, negation !(x>0)
  • 22. Condition1 Condition2 && || true true true true true false false true false true false true false false false false
  • 23. Logical Operator using these operators, we can form a Boolean Expression that can be either true or false. Boolean expressions evaluate to integer values. The value 0 means 'false' and all non-zero value means 'true'. There are also two literal constants used with bool variables: true and false.
  • 24. In Class Exercise What is the difference between x = 3 and x == 3? Given a = 2, b = 5, c = 7 determine the value for the following expressions either TRUE or FALSE: : 1. a < 2 && b > 5 2. c >= a 3. a < b || b > c 4. c != 10 5. a == 25
  • 25. Operator Precedence Determines the order in which mathematical computations and operations are performed in an expression.
  • 27. Operator Precedence Given a=b = 5, c = 2; solve the following expression a+b*2/c–5 =5+5*2/2–5 = 5 + 10 / 2 – 5 =5+5–5 = 10 – 5 =5
  • 28. In Class Exercise Given x = 10, y = 7, z = 2; solve the following expression x + 2y – z x / z – x * x + y x * y % z 5 ( x + y + z ) – x / z ++x*y – y*z
  • 29. Type conversion/Type Casting Is the conversion of a value of one data type into another data type. Typecasting consist of two types:  Implicit conversion  Explicit conversion
  • 30. Implicit conversion A value of data type with lower range is assigned to a variable of data type with higher range Lower range to higher range
  • 31. Example #include<iostream> using namespace std; OUTPUT void main() Result: 4.2555 { double x; // Size is 8 bytes int y=2; //Size is 4 bytes float z=2.2555f; x=y+z; //Implicit conversion cout<<"Result: "<<x; }
  • 32. Explicit conversion A value of data type with higher range is assigned to a variable of data type with lower range Higher range to lower range Explicit conversion leads to data loss.
  • 33. Example #include<iostream> using namespace std; OUTPUT void main() The total value is:300 { int total_value; double book1 = 300.46; total_value=(int)book1; // Explicit conversion cout<<"The total value is:"<<total_value; }
  • 34. Exercise Write a program to declare a variable d of type double. Assign the value 102.5789 to d. Declare a float variable fl. Assign the value of d to fl by performing an explicit conversion and print the value on the screen.
  • 35. Answer #include<iostream> using namespace std; void main() OUTPUT { Value of the float variable fl = 102.579 double d=102.5789; float fl; fl=(float)d; // Explicit conversion cout<<"Value of the float variable fl = "<<fl; }
  • 36. Summary In this presentation, you learnt the following: Operator is a symbol that instructs compiler to perform an operation or action. Typecasting is the conversion of data type of the value of a variable into another. There are two types of casting: implicit conversion and explicit conversion.
  • 37. FP 201 – Programming Fundamentals with C++ Input Output Statements
  • 38. Learning outcome By the end of the course, students should be able to: Identify the syntax use for input and output Write program that use the input and output statements
  • 39. Input/Output Statements The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared. cout to output data to the screen and cin to input data from the keyboard.
  • 40. Standard Output standard output: need to use cout, followed by the insertion operator (<<) eg: cout << "Hello World!"; // Outputs "Hello World!" on the screen cout << 1299; // Outputs the number 1299 on the screen cout << x; // Outputs the content of x on the screen insertion operator (<<) may be used more than once in a single statement: cout << "Hi, " << "this is " << "a simple C++ statement";
  • 41. Standard Output Note that, cout does not add a line break. For example: cout << "Computer Programming "; cout << "is important for engineers."; will be output as: Computer Programming is important for engineers.
  • 42. Standard Output A line break can be added by inserting a 'n' character or a using a endl manipulator. cout << "Computer Programming n"; cout << "is important for engineers."; cout << "Computer Programming " << endl; cout << "is important for engineers."; These two produce the following (same) output: Computer Programming is important for engineers.
  • 43. Standard Input  Handling the standard input is done by applying the overloaded operator of extraction (>>) on the cin stream  Value can be input from the keyboard and assigned to a variable x as follows: cin >> x;  and two variables can assigned as any of the follows: cin >> x; cin >> y; cin >> x >> y;
  • 44. Code Example // Calculates the sum of two integers #include <iostream> using namespace std; int main() { int a, b, total; cout << "Enter two integers to be added: "; cin >> a >> b; total = a + b; cout << "The sum is " << total << endl; return 0; }
  • 45. Code Example // Calculates the area of a triangle // given an input of the triangle base and height. #include <iostream> int main() { float base, height, area; std::cout << "Enter the base and height of the triangle: "; std::cin >> base >> height; area = 0.5*base*height; std::cout << "The area of the triangle is " << area << std::endl; }
  • 46. Q&A Develop a program that can calculate the total and average of four numbers that is input by user.
  • 47. #include <iostream> using namespace std; int main() { int a, b, c, d, total, average; cout << "Enter four integers to be added: "; cin >> a >> b>>c>>d; total = a + b + c + d; average = total/4; cout << "The sum is " << total << endl; cout<< "The average is "<< average << endl; return 0; }
  • 48. Summary In this presentation, you learnt the following: cout to output data to the screen cin to input data from the keyboard. cout use insertion operator (<<) cin use overloaded operator of extraction (>>)

Editor's Notes

  • #16: k=84 y=98.55 y=21 a= i=8
  • #25: a &lt; 2 &amp;&amp; b &gt; 5 : FALSE c &gt;= a : TRUE a &lt; b || b &gt; c : FALSE c != 10: TRUE a == 25: FALSE
  • #29: x + 2y – z=22 x / z – x * x + y=-88 x * y % z=0 5 ( x + y + z ) – x / z:90 ++x*y – y*z=63