Manual For 3rd Lab
Manual For 3rd Lab
Lahore Campus
========================================================
Email: [email protected]
1. cout (<<)
• cout is used to output various data types like characters, strings,
integers, floats, octals, and hexadecimal values onto the screen.
• Syntax: cout<<” The data you want to display.”;
\b Backspace
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
3. cin (>>)
• cin is used to take input from the user, which is then stored in a variable.
• The user types a value, then presses Enter, and cin assigns this value to the
specified variable.
• Syntax: cin>>variable;
Example Program
Here's a program demonstrating cout, cin, escape sequences, and comments:
#include <iostream>
int main() {
cout << "Alert Sound:\a (if sound is enabled)\n"; // \a triggers an alert sound
cout << "Quotation Marks: \"This is in quotes\"\n"; // \" for double quotes
cout << "Single Quote: \'Single quoted text\'\n"; // \' for single quotes
int age;
cout << "Your age is: " << age << "\n"; // Display the entered age
/* Multi-line comment:
*/
return 0;
Variables:
Variables are just like containers to store data values and are named storage
locations in memory.
• Whitespace and special symbols are not allowed other than underscore.
• Memory space is not allocated for a variable while declaration. It happens only
on variable definition.
data_type variable_name ;
Department of Computer Science (SST), UMT.
6
variable_name = value;
OR
• Example:
int age;
age= 19;
OR
Data Types:
• Data types specify the type of data a variable can hold.
• Common data types:
o int: Integer values (e.g., 5, -10)
o float: Floating-point numbers with decimals (e.g., 3.14, -2.5)
o double: Double-precision floating-point numbers (more precise than
float)
o char: Single character (e.g., 'a', 'Z')
o bool: Boolean values (true or false)
o string: Sequence of characters (e.g., "Hello")
• Constants:
o Constants are fixed values that do not change during the program.
o Syntax: const data_type variable_name = value;
o Example: const int DAYS_IN_WEEK = 7;
Department of Computer Science (SST), UMT.
7
Examples:
Program Output = 4.
Program Output = 6.
a. Arithmetic Operators:
i. Binary Operators: These operators operate or work with two operands (+,
-, /, *, %).
ii. Unary Operators: These operators operate or work with a single operand
(Increment++, Decrement--).
Binary Operators
(+, -,*, /, %)
Purpose: Perform basic arithmetic operations.
Example:
int a = 10, b = 5;
cout << "Addition: " << (a + b) << endl; // 15
cout << "Subtraction: " << (a - b) << endl; // 5
cout << "Multiplication: " << (a * b) << endl; // 50
cout << "Division: " << (a / b) << endl; // 2
cout << "Modulus: " << (a % b) << endl; // 0
Unary Operators
(++,--)
Purpose: Increase or decrease a variable’s value by 1.
Example:
int x = 5, y=6;
cout << "Post-increment: " << x++ << endl; // 5 (x becomes 6) Post Increment
cout << "Pre-increment: " << ++x << endl; // 7 Pre Increment
cout << "Post-decrement: " << y-- << endl; // 6 (y becomes 5) Post Decrement
cout << "Pre-decrement: " << --y << endl; // 4 Pre Decrement
b. Relational Operators:
These operators are used for the comparison of the values of two operands. It
includes (>, <, >=, <=, !=, ==).
For example: ‘>’ checks if one operand is greater than the other operand or not,
etc. The result returns a Boolean value, i.e., true (1) or false (0).
Example: Suppose A=5 and B=10.
OPERATORS DESCRIPTION EXAMPLE
c. Logical Operators:
These operators are used to combine two or more conditions or constraints or to
complement the evaluation of the original condition in consideration.
It includes (&&, ||, !). The result returns a Boolean value, i.e., true (1) or false
(0).
Example: Suppose if X=10 and Y=5.
OPERATORS DESCRIPTION EXAMPLE
d. Bitwise Operators:
These operators are used to perform bit-level operations on the operands. The
operators are first converted to bit-level and then the calculation is performed on
the operands. Mathematical operations such as addition, subtraction,
multiplication, etc. can be performed at the bit level for faster processing.
It includes (Binary AND -> &, Binary OR -> |, Binary XOR ^, Left Shift <<,
Right Shift >>, One’s Complement ~).
Note: Only char and int data types can be used with Bitwise Operators.
e. Assignment Operators:
These operators are used to assign value to a variable. The left side operand of
the assignment operator is a variable and the right-side operand of the
assignment operator is a value. The value on the right side must be of the same
data type as the variable on the left side otherwise the compiler will raise an
error.
It includes (=, +=, -+, *=, /+).
Example Program
Here’s an example to find the maximum of two numbers using the ternary
operator:
#include <iostream>
int main() {
int num1 = 10;
cout << "The maximum number is: " << max << endl;
return 0;
}
Output:
Arithmetic Expressions:
Arithmetic expressions contain a combination of the arithmetic operations
including brackets:
x = z – (a + b / 2) + w * y
To evaluate the expression, we need to follow the precedence rule which is as
follows:
• ( ) expression within parentheses.
• +, - unary operators (plus and minus sign).
Department of Computer Science (SST), UMT.
15
Examples:
Here are the example programs:
Understanding Precedence
Output:
Understanding Associativity
Output:
Lab Tasks:
Task 1: Write a C++ program that uses cin and cout to display your name, favorite
color, and a simple message. (also use comments and any two escape characters)
Task 3: Write a program that take six subjects marks from User and Display the
sum of all marks. (also use const like fixed value that total marks = 600)
Task 4: Write a C++ program taking two values as inputs from the user and display
the results for all the basic arithmetic operations performed on them.
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
Task 5: Write a C++ program to find the minimum of two numbers that are taken
from user by using ternary operator.
Task 6: What will be the output of the following both program: (Do it on Paper
don’t use compiler)
Home Tasks:
Task 1: Write a C++ program to find the Area and Perimeter of a Rectangle.
F = 1.8 x C + 32.
Task 3: Write a C++ program that reads three integers representing hours, minutes,
and seconds of a time. Then it should calculate the equivalent time in seconds.
Task 4: What will be the output of the following program: (Do it on Paper don’t use
compiler)
(1)
(2)
(3)