SlideShare ist ein Scribd-Unternehmen logo
1 von 7
F2037 PROGRAMMING FUNDAMENTAL OF C++


LAB 2: OPERATORS AND EXPRESSION

Learning Outcome:

By the end of this lab, students should be able to:
•   Understand operators, operator’s precedence and expression.


Theory/ Topics

•   A simple C++ program is similar to a C program. In C++ programs
    the statements to be executed are contained inside the function.

•   In operators and expressions, student must know about:
    a) Arithmetic operators ( +, -, *, /, % )
          The five arithmetical operations supported by the C++
             language are:
                               + addition
                               - subtraction
                               * multiplication
                               / division
                               % modulus

    b) Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |
       =)

    c) Increment and decrement (++, --)
        Shortening even more some expressions, the increase
        operator (++) and the decrease operator (--) increase or
        reduce by one the value stored in a variable. They are
        equivalent to +=1 and to -=1, respectively.

                    Example 1                     Example 2
           B=3;                          B=3;
           A=++B;                        A=B++;
           // A contains 4, B contains 4 // A contains 3, B contains 4


           d) Relational and equality operators (==, !=, >, <, >=, <= )

                                                                         1
F2037 PROGRAMMING FUNDAMENTAL OF C++


        In order to evaluate a comparison between two
        expressions we can use the relational and equality
        operators. The result of a relational operation is a
        Boolean value that can only be true or false, according
        to its Boolean result.

   e) Logical operators ( !, &&, || )
       The Operator ! is the C++ operator to perform the
       Boolean operation NOT, it has only one operand,
       located at its right, and the only thing that it does is to
       inverse the value of it, producing false if its operand is
       true and true if its operand is false. Basically, it returns
       the opposite Boolean value of evaluating its operand.

    Precedence of operators
When writing complex expressions with several operands, we
must follow the precedence which is what operand is evaluated
first and which later. For example, in this expression:

                         a=5+7%2

    7%2 is evaluated first, then followed by operator +

Activity 2A

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2A.cpp.

#include <iostream>
using namespace std;
int main()
{
      int class1 = 100;
      int class2 = 200;
      int class3 = 300;
      int class4 = 400;
      int class5 = 500;


                                                                 2
F2037 PROGRAMMING FUNDAMENTAL OF C++


      int sum = 0;
      double average;
      sum = class1 + class2 + class3 + class4 +
      class5;
      average = sum/5;
      cout << " Sum = " << sum << endl;
      cout << " Average = " << average << endl;
      return 0;
}

Activity 2B

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2B.cpp.

#include <iostream>
using namespace std;

void main()
{
      int x = 180, y = 200;
      y = x++;
     cout << " x : " << x << endl << " y : " << y <<
     endl;
}

Activity 2C

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2C.cpp.

#include <iostream>
using namespace std;



                                                         3
F2037 PROGRAMMING FUNDAMENTAL OF C++


void main()
{
      int x = 180, y = 200;
      y = ++ x;
      cout << " x : " << x << endl << " y : "<<
      y << endl;
}

Activity 2D

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2D.cpp.

#include <iostream>
using namespace std;

void main()
{
      double p     = 12.5;
      double q     = 3.234;
      p *= q -     1;
      q += p +     1;
cout << " p is     " << p << endl << " q is " <<
q << "n";
}

Lab 2E

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2E.cpp.

// Demonstrate the modulus operator.
#include <iostream>
using namespace std;


                                                         4
F2037 PROGRAMMING FUNDAMENTAL OF C++


int main()
{
      int x, y;
      x = 10;
      y = 3;
      cout << x << " / " << y << " is " << x / y <<
      " with a remainder of " << x % y << "n";
      x = 1;
      y = 2;
      cout << x << " / " << y << " is " << x / y <<
      "n" << x << " % " << y << " is " << x % y;
      return 0;
}

Lab 2F

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2F.cpp.

// Demonstrate the relational and logical operators.
#include <iostream>
using namespace std;
int main()
{
      int i, j;
      bool b1, b2;
      i = 10;
      j = 11;
      if(i < j) cout << "i < jn";
      if(i <= j) cout << "i <= jn";
      if(i != j) cout << "i != jn";
      if(i == j) cout << "this won't executen";
      if(i >= j) cout << "this won't executen";
      if(i > j) cout << "this won't executen";

         b1 = true; b2 = false;
         if(b1 && b2) cout << "this won't executen";



                                                         5
F2037 PROGRAMMING FUNDAMENTAL OF C++


       if(!(b1 && b2)) cout << "!(b1 && b2) is
       truen";
       if(b1 || b2) cout << "b1 || b2 is
       truen";
       return 0;
}

Lab 2G

Procedure :

Step 1: Type the programs given below
Step 2: Compile and run the program. Write the output.
Step 3: Save the program as Lab2G.cpp.

#include<iostream>
using namespace std;

void main()
{
      int i;
      for(i=1;i<=10;i++)
      cout<<i<<" /2 is: " << (float) i/2 << "n";
}


LAB EXERCISE

1. Write the C++ expression for the following
    mathematical statement

    a. y=(x-2)(x+3)
    b. min = a + b + c + d + e
                       5
                                                  (2 marks)

2. Given the values x=5, y=5 and c=3. Write a program
    to calculate the value of z and display the output of z



                                                              6
F2037 PROGRAMMING FUNDAMENTAL OF C++


         z = xy % c + 10 / 2y + 5;                   (4 marks)

3. Based on the flowchart, find the values for a and b. Write a
   program to calculate the values and display the output
                                                       (4
   marks)


                           start

                      x=12, y=8,z=5


                         a=x*y-z


                     b = (6*a/2+3-z)/2



                        print a,b


                            end

CONCLUSION
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
____________________________________________________
___________________________________________________



                                                             7

Weitere ähnliche Inhalte

Was ist angesagt?

Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 studrohassanie
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language samt7
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++ Bharat Kalia
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshopneosphere
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)Mansi Tyagi
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in CColin
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharDreamtech Labs
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1Zaibi Gondal
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 

Was ist angesagt? (20)

C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
C++ book
C++ bookC++ book
C++ book
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
 
C language
C languageC language
C language
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C++
C++C++
C++
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in C
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 

Andere mochten auch

SSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In HindiSSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In Hindikusumafoundation
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingHaris Bin Zahid
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented languagefarhan amjad
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesGuido Wachsmuth
 
Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015Saurabh Singh Negi
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Languagedheva B
 
हिन्दी व्याकरण
हिन्दी व्याकरणहिन्दी व्याकरण
हिन्दी व्याकरणChintan Patel
 
OOPs concept and implementation
OOPs concept and implementationOOPs concept and implementation
OOPs concept and implementationSandeep Kumar P K
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programmingNgeam Soly
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic Manzoor ALam
 
Vakya parichay
Vakya parichayVakya parichay
Vakya parichaysonia -
 

Andere mochten auch (20)

Unit i
Unit iUnit i
Unit i
 
Oops
OopsOops
Oops
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
SSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In HindiSSRP Self Learning Guide Maths Class 10 - In Hindi
SSRP Self Learning Guide Maths Class 10 - In Hindi
 
Oops Concepts
Oops ConceptsOops Concepts
Oops Concepts
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015Cbse class 10 hindi course b model answers by candidates 2015
Cbse class 10 hindi course b model answers by candidates 2015
 
Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
हिन्दी व्याकरण
हिन्दी व्याकरणहिन्दी व्याकरण
हिन्दी व्याकरण
 
OOPs concept and implementation
OOPs concept and implementationOOPs concept and implementation
OOPs concept and implementation
 
Chapter3: fundamental programming
Chapter3: fundamental programmingChapter3: fundamental programming
Chapter3: fundamental programming
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
कारक
कारककारक
कारक
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Vakya parichay
Vakya parichayVakya parichay
Vakya parichay
 
कारक(karak)
कारक(karak)कारक(karak)
कारक(karak)
 

Ähnlich wie Labsheet2

Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201rohassanie
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introductionAnanda Kumar HN
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3rohassanie
 
Python programming workshop session 4
Python programming workshop session 4Python programming workshop session 4
Python programming workshop session 4Abdul Haseeb
 
C++ Programming.pdf
C++ Programming.pdfC++ Programming.pdf
C++ Programming.pdfMrRSmey
 
Esg111 midterm review
Esg111 midterm reviewEsg111 midterm review
Esg111 midterm reviewmickeynickey1
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzationKaushal Patel
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++sunny khan
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 solIIUM
 
Numerical Methods for Engineers 6th Edition Chapra Solutions Manual
Numerical Methods for Engineers 6th Edition Chapra Solutions ManualNumerical Methods for Engineers 6th Edition Chapra Solutions Manual
Numerical Methods for Engineers 6th Edition Chapra Solutions Manualwebavaq
 

Ähnlich wie Labsheet2 (20)

Lab 1
Lab 1Lab 1
Lab 1
 
C++ Question
C++ QuestionC++ Question
C++ Question
 
901131 examples
901131 examples901131 examples
901131 examples
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3FP 201 Unit 2 - Part 3
FP 201 Unit 2 - Part 3
 
Python programming workshop session 4
Python programming workshop session 4Python programming workshop session 4
Python programming workshop session 4
 
C++ Programming.pdf
C++ Programming.pdfC++ Programming.pdf
C++ Programming.pdf
 
Esg111 midterm review
Esg111 midterm reviewEsg111 midterm review
Esg111 midterm review
 
Operators-computer programming and utilzation
Operators-computer programming and utilzationOperators-computer programming and utilzation
Operators-computer programming and utilzation
 
Few Operator used in c++
Few Operator used in c++Few Operator used in c++
Few Operator used in c++
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Numerical Methods for Engineers 6th Edition Chapra Solutions Manual
Numerical Methods for Engineers 6th Edition Chapra Solutions ManualNumerical Methods for Engineers 6th Edition Chapra Solutions Manual
Numerical Methods for Engineers 6th Edition Chapra Solutions Manual
 

Mehr von rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012rohassanie
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2) rohassanie
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2rohassanie
 

Mehr von rohassanie (20)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
 

Kürzlich hochgeladen

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Kürzlich hochgeladen (20)

DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

Labsheet2

  • 1. F2037 PROGRAMMING FUNDAMENTAL OF C++ LAB 2: OPERATORS AND EXPRESSION Learning Outcome: By the end of this lab, students should be able to: • Understand operators, operator’s precedence and expression. Theory/ Topics • A simple C++ program is similar to a C program. In C++ programs the statements to be executed are contained inside the function. • In operators and expressions, student must know about: a) Arithmetic operators ( +, -, *, /, % )  The five arithmetical operations supported by the C++ language are: + addition - subtraction * multiplication / division % modulus b) Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, | =) c) Increment and decrement (++, --) Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Example 1 Example 2 B=3; B=3; A=++B; A=B++; // A contains 4, B contains 4 // A contains 3, B contains 4 d) Relational and equality operators (==, !=, >, <, >=, <= ) 1
  • 2. F2037 PROGRAMMING FUNDAMENTAL OF C++ In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. e) Logical operators ( !, &&, || ) The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. Precedence of operators When writing complex expressions with several operands, we must follow the precedence which is what operand is evaluated first and which later. For example, in this expression: a=5+7%2 7%2 is evaluated first, then followed by operator + Activity 2A Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2A.cpp. #include <iostream> using namespace std; int main() { int class1 = 100; int class2 = 200; int class3 = 300; int class4 = 400; int class5 = 500; 2
  • 3. F2037 PROGRAMMING FUNDAMENTAL OF C++ int sum = 0; double average; sum = class1 + class2 + class3 + class4 + class5; average = sum/5; cout << " Sum = " << sum << endl; cout << " Average = " << average << endl; return 0; } Activity 2B Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2B.cpp. #include <iostream> using namespace std; void main() { int x = 180, y = 200; y = x++; cout << " x : " << x << endl << " y : " << y << endl; } Activity 2C Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2C.cpp. #include <iostream> using namespace std; 3
  • 4. F2037 PROGRAMMING FUNDAMENTAL OF C++ void main() { int x = 180, y = 200; y = ++ x; cout << " x : " << x << endl << " y : "<< y << endl; } Activity 2D Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2D.cpp. #include <iostream> using namespace std; void main() { double p = 12.5; double q = 3.234; p *= q - 1; q += p + 1; cout << " p is " << p << endl << " q is " << q << "n"; } Lab 2E Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2E.cpp. // Demonstrate the modulus operator. #include <iostream> using namespace std; 4
  • 5. F2037 PROGRAMMING FUNDAMENTAL OF C++ int main() { int x, y; x = 10; y = 3; cout << x << " / " << y << " is " << x / y << " with a remainder of " << x % y << "n"; x = 1; y = 2; cout << x << " / " << y << " is " << x / y << "n" << x << " % " << y << " is " << x % y; return 0; } Lab 2F Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2F.cpp. // Demonstrate the relational and logical operators. #include <iostream> using namespace std; int main() { int i, j; bool b1, b2; i = 10; j = 11; if(i < j) cout << "i < jn"; if(i <= j) cout << "i <= jn"; if(i != j) cout << "i != jn"; if(i == j) cout << "this won't executen"; if(i >= j) cout << "this won't executen"; if(i > j) cout << "this won't executen"; b1 = true; b2 = false; if(b1 && b2) cout << "this won't executen"; 5
  • 6. F2037 PROGRAMMING FUNDAMENTAL OF C++ if(!(b1 && b2)) cout << "!(b1 && b2) is truen"; if(b1 || b2) cout << "b1 || b2 is truen"; return 0; } Lab 2G Procedure : Step 1: Type the programs given below Step 2: Compile and run the program. Write the output. Step 3: Save the program as Lab2G.cpp. #include<iostream> using namespace std; void main() { int i; for(i=1;i<=10;i++) cout<<i<<" /2 is: " << (float) i/2 << "n"; } LAB EXERCISE 1. Write the C++ expression for the following mathematical statement a. y=(x-2)(x+3) b. min = a + b + c + d + e 5 (2 marks) 2. Given the values x=5, y=5 and c=3. Write a program to calculate the value of z and display the output of z 6
  • 7. F2037 PROGRAMMING FUNDAMENTAL OF C++ z = xy % c + 10 / 2y + 5; (4 marks) 3. Based on the flowchart, find the values for a and b. Write a program to calculate the values and display the output (4 marks) start x=12, y=8,z=5 a=x*y-z b = (6*a/2+3-z)/2 print a,b end CONCLUSION ____________________________________________________ ____________________________________________________ ____________________________________________________ ____________________________________________________ ____________________________________________________ ___________________________________________________ 7