0% found this document useful (0 votes)
41 views7 pages

Lab 4

This lab report focuses on C++ operators, detailing various tasks that enhance understanding of their functionality, including conditional checks, arithmetic operations, and character validation. The report outlines specific tasks such as using ternary operators, performing arithmetic operations, and validating expressions, providing code snippets and expected outputs for each. Overall, the lab emphasizes the importance of operators in effective data manipulation within C++ programming.

Uploaded by

usernotfound
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views7 pages

Lab 4

This lab report focuses on C++ operators, detailing various tasks that enhance understanding of their functionality, including conditional checks, arithmetic operations, and character validation. The report outlines specific tasks such as using ternary operators, performing arithmetic operations, and validating expressions, providing code snippets and expected outputs for each. Overall, the lab emphasizes the importance of operators in effective data manipulation within C++ programming.

Uploaded by

usernotfound
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LAB REPORT

_____________________________________________________________________________

Programming Fundamental Lab


CS-102L

BS Robotics & Automation


Submitted By

Rashna Nadeem
24-RA-015

Section: RA – 01

Department of Electrical Engineering


HITEC University
Heavy Industries Taxila Education City
Lab # 04

C++ Operators
Learning Objectives
1. To run and analyze different C++ programs involving various operators and data type
conversions.

2. To implement C++ programs using different types of operators effectively.

Equipment and Components


Dev-C++ 5.0 Beta 9.2 (4.9.9.2)

Introduction to C++ Operators


Operators in C++ are special symbols that perform operations on variables and values. They
are essential for manipulating data and include arithmetic, relational, logical, bitwise,
assignment, and ternary operators. For example, the + operator adds two numbers, while the
ternary operator ?: provides a concise way to perform conditional checks. Understanding
operators is fundamental to writing effective C++ programs.
Lab Tasks
Task 1: Check Whether a Number is Positive, Negative, or Zero Using Ternary
Operators

Code:

Output:

Page | 2
1. Uses nested ternary operators for concise conditional checking.

2. Checks if number is greater than zero → prints "Positive".

3. Else checks if number is less than zero → prints "Negative".

4. Otherwise, prints "Zero".

Task 2: Perform All Arithmetic Operations on Two Inputs

Code:

Output:

1. Takes two inputs from the user.

2. Performs addition, subtraction, and multiplication directly.

3. Checks for division by zero before dividing.

4. Performs modulus after casting inputs to integers.

Page | 3
Task 3: Compute Average of Three Numbers

Code:

Output:

1. Takes three numbers as input.

2. Adds all three numbers.

3. Divide the sum by 3 to calculate the average.

4. Prints the average.


Task 4: Check if the Entered Character is 'a or 'A'

Code:

Output:

Page | 4
1. Takes a character input.

2. Uses logical OR (||) to check if character is 'a' or 'A'.

3. Prints appropriate message based on condition.


Task 5: Value of total and Explanation

Code:

Output:

1. Initialize total to 0.

2. Add 5 → total becomes 5.

3. Subtract 1 → total becomes 4.

4. Multiply by (total + 4) → 4 * 8 = 32.

5. Final value of total is 32.


Page | 5
Task 6: Value of x and y After Execution
Code:

Output:

1. Initial values:
x = 5, y = 9
2. Expression x -= ++y * 4:
 First, ++y increments y before use: y = 10
 Then, ++y * 4 becomes 10 * 4 = 40
 Now, x -= 40 means x = x - 40 = 5 - 40 = -35
3. Final values:
 x = -35
 y = 10
Task 7: C++ Program to Validate the Expression !(x > 0)
Code:

Output:

Page | 6
1. Input: The program prompts the user to enter a number.

2. Condition Check: It checks if !(x > 0) is true, which means x is either negative or
zero.

3. Output: It prints a message indicating whether the expression is true or false based on
the value of x.

Conclusion:
This lab report explored various C++ operators through tasks that enhanced our understanding of
their functionality. We used the ternary operator to assess the sign of a number, performed
arithmetic operations while managing special cases, and calculated averages. We also checked
character input with logical operators, evaluated arithmetic expressions to grasp operator
precedence, and validated the expression `!(x > 0)` for negative values. Overall, these tasks
provided valuable insights into C++ operators and effective data manipulation techniques.

Marks Distribution:

Presentation(Format(0.5)+Title(0.5)+ Calculations Observation /


Conclusion(1)+Theory (2) /Coding Program Total
+Observation(1)) Results
15
5 5 5
Total

Obtained

Lab Instructor

Total Marks:

Marks Obtained:

Page | 7

You might also like