Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

ITC C++ Programming LAB8

Uploaded by

azhan7257
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ITC C++ Programming LAB8

Uploaded by

azhan7257
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of By: Engr.

Farhan Memon
Electrical Engineering Introduction to Computers & C++
Mehran University of Engineering Year 1ST Semester 1st
& Technology Batch 17 Duration 03
Jamshoro Hours

Lab 8: Working with Operators in C++

Outlines

· Operators
· Arithmetic Operators
· Increment/Decrement Operators
· Arithmetic Assignment Operators
· Relational Operators
· Logical Operators
· Lab tasks

Requirements
· Code::Block IDE

1
OPERATORS
Operators are used to perform calculations or operations on the operands.

Operators in c++:
Arithmetic Operators

Increment/Decrement Operators

Arithmetic Assignment Operators

Relational Operators

Logical Operators

ARITHMETIC OPERATORS
Arithmetic operators are operators used to perform basic mathematical operations.

2
Program
#include <iostream>

using namespace std;

int main()

double x, y;

cout << "\nEnter two floating-point values: ";

cin >> x >> y;

cout << "The average of the two numbers is: " << (x + y)/2.0 << endl;

return 0;

INCREMENT/DECREMENT OPERATORS

The increment operator ++ modifies the operand by adding 1 to its value.

++i is incremented first and the new value of i is then applied (prefix)

i++ the original value of i is applied before i is incremented (postfix)

The decrement operator -- modifies the operand by subtracting 1 to its value.

--i is decremented first and the new value of i is then applied (prefix)

i-- the original value of i is applied before i is decremented (postfix)

3
Program
#include <iostream>

using namespace std;

int main()

int i=2, j=8;

cout << i++ << endl; // Output: 2

cout << i << endl; // Output: 3

cout << ++i << endl; // Output: 4

cout << j-- << endl; // Output: 8

cout << j << endl; // Output: 7

cout << --j << endl; // Output: 6

return 0;

ARITHMETIC ASSIGNMENT OPERATORS


A simple assignment operator (=) used to assign the value of a variable.

z = 7.5;

y = z;

x = 2.0 + 4.2 * z;

Arithmetic assignment operators (Combination of arithmetic +, -,*,/ and assignment operator)


that simultaneously perform an arithmetic operation and an assignment. +=, -=, *=, /=, and %=.

i += 3; is equivalent to i = i + 3;

i *= j + 2; is equivalent to i = i * (j+2)

4
Program
#include <iostream>

using namespace std;

int main ()

int a=3;

a+=2;

cout <<" Value of a is changed into = "<< a;

return 0;

RELATIONAL OPERATORS
These operators establish a relationship between operands and are used to compare two
operands.

5
Program

#include <iostream>

using namespace std;


int main() {

int a = 21;
int b = 58;

if(a>b)
{
cout << " a is greater than b" << endl ;
}

else
{
cout<<"b is greater than a" <<endl;
}
return 0;
}

LOGICAL OPERATORS
They are used to combine two different expressions together. The logical operators are AND
(&&) , OR (||) and NOT (!).

6
Program

#include <iostream>

using namespace std;

int main() {

int age;

cout << "Enter your age: ";

cin >> age;

if(age >= 35 && age <= 80)

{ cout << "You're between 35 and 80" << endl; }

else

{ cout << "Sorry, Your Age is not in the Range" << endl; }

return 0;

7
LAB TASKS
1. Write down a program using OR (||) operator in which a user enters a character at
run-time to find either the given character is vowel or not.
(Hint: use if-else structure).

2A person enters the bank and stands in the queue to get his salary. When his turn
comes, he requests the cashier that I need my salary with minimum notes and coins.
Write a program in C++ for the cashier that first asks the cashier to enter the salary
amount and then displays the number of notes and coins of (Rs. 5000, Rs. 1000, Rs. 100,
Rs. 50, Rs. 20, Rs. 10, Rs. 5, Rs. 2 and Re. 1).

SUBMISSION:
In Hard-Format (manual along with exercise), will be checked in the next Lab

The End

You might also like