Lab 1 To Lab 3 Complete

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 53

LABORATORY MANUAL

Computer Engineering Department

Computer Programming

Submitted by
Muhammad Hamza | 62908

Lab Instructor

Engr. Rehmat Ullah


Lecturer, Department of Computer Engineering,
Faculty of Information & Communication Technology,
BUITEMS, Quetta.

Session
Spring – 2024
LAB MANUAL COMPUTER PROGRAMMING

CERTIFICATE

This is certification that Mr./Miss. Muhammad Hamza, bearing CMS ID #

62908, has successfully completed the laboratory manual of computer

programming in his/her 2nd semester of BS (Computer Engineering),

Session Fall 2024, under the supervision of his or her lab instructor, Engr.

Rehmat Ullah.

_________________ __________________

Lab Manual Marks Instructor’s Signature


LAB MANUAL COMPUTER PROGRAMMING

CLO and PLOs Mapping

Course Learning Outcomes (Laboratory)


Sr.
Taxonomy
No CLO Domain PLO
Level
.
Apply programming skills to a Psychomotor P3 PLO-2: Problem
1 particular condition and identify Analysis
logical and syntax errors.
Designing, implementing, and Psychomotor P3 PLO-3: Design
2 debugging small-medium scale and Development
programs to give problem solutions. of Solutions.
LAB MANUAL COMPUTER PROGRAMMING

LIST OF EXPERIMENTS

Sr. No Experiments
1 PRIMITIVE DATA TYPES AND VARIABLES
2 IDENTIFIERS AND KEYWORDS
3 OPERATORS IN C++
4 CONDITIONAL STATEMENTS (if, else, else if)
5 SWITCH STATEMENTS
6 RESTAURANT BILL CALCULATOR (Open-Ended Lab)
7 FOR LOOPS IN C++
8 WHILE LOOPS IN C++
9 DO WHILE LOOPS IN C++
10 NESTED LOOPS IN C++
11 FUNCTIONS IN C++
12 ARRAYS IN C++
13 POINTERS IN C++
LIBRARY BOOK INVENTORY MANAGEMENT SYSTEM
14
USING C++ (Open-Ended Lab)
15 FILE OPERATIONS IN C++

Note: The above list of experiments is common for this subject in all concerned departments of
FICT, however, the subject instructor shall choose the relevant experiments among this list
according to the program requirements.
LAB MANUAL COMPUTER PROGRAMMING

DOCUMENT VERSION DETAILS

Version Date Author Rationale

First Draft
1.0 01 January 2021 Engr. Syed Asad Ullah
(Lab Format Design)
Reviewed
1.1 01 February 2021 Engr. Syed Umair Shah
(Common lab titles updated)
Modified
(Program relevant experiments
1.2 5 February 2021 Engr. Syed Umair Shah
are selected from the given
common list of experiments)
Document adopted and
1.3 10 February 2021 Engr. Syed Umair Shah
implemented as a lab subject
1.4 01 May 2022 Engr. Rehmat Ullah Updated
1.5 07 March 2023 Engr. Muhammad Shahzeb Khan Updated
1.6 05 April 2024 Engr. Rehmat Ullah Updated
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 1 DD/MM/YYYY

PRIMITIVE DATA TYPES AND VARIABLES


Lab outcomes:
After completing this lab, students will be able to:
 Understand and implement data types.
 Understand and implement variables.
Corresponding CLO and PLO:
 CLO-1, PLO-2 (Engineering Knowledge)
Theory:
Primitive Data Types: Primitive data types in C++ are basic data types that are used to define
variables and their values. These data types are built into the C++ language and are fundamental
for performing arithmetic and logical operations on them. C++ has five primitive data types,
namely:
1. Integers (int): Integers are used to store whole numbers without any fractional part. In
C++, the int data type is used to define integer variables. The size of an integer depends
on the system architecture, but it is usually 4 bytes. Here's an example:
int age = 25;
In this example, the variable "age" is defined as an integer and initialized with the value 25.
2. Floating-point numbers (float and double): Floating-point numbers are used to store
decimal numbers. In C++, the float data type is used to define single-precision floating-
point numbers, while the double data type is used to define double-precision floating-
point numbers. The size of a float is 4 bytes, while the size of a double is 8 bytes.
The main difference between single precision and double precision floats is their
precision, or the number of significant digits they can represent. Single precision floats
have a precision of roughly 7 decimal digits, while double precision floats have a
precision of roughly 15-16 decimal digits. This means that double precision floats can
represent numbers with greater accuracy and range than single precision floats.
Here's an example:
float weight = 65.5;
double height = 1.75;
In this example, the variable "weight" is defined as a float and initialized with the value 65.5,
while the variable "height" is defined as a double and initialized with the value 1.75.
LAB MANUAL COMPUTER PROGRAMMING

3. Characters (char): Characters are used to store single characters such as 'a', 'b', 'c', etc.
In C++, the char data type is used to define character variables. They are stored as ASCII
values in memory and take 1 byte of memory space. Here's an example:
char grade = 'A';
In this example, the variable "grade" is defined as a char and initialized with the character 'A'.
4. Booleans (bool): Booleans are used to store true or false values. In C++, the bool data
type is used to define Boolean variables. They take 1 byte of memory space. Here's an
example:
bool isMarried = true;
In this example, the variable "isMarried" is defined as a bool and initialized with the value true.
5. Void (void): Void is a special data type that is used to specify that a function does not
return any value. Here's an example:
void printHelloWorld() {
std::cout << "Hello World!" << std::endl;
}
In this example, the function "printHelloWorld" is defined with a void return type, indicating that
it does not return any value. When called, it prints the message "Hello World!" to the console.
Variable: A variable is a named storage location that can hold a value of a particular data type.
Declaring Variable: To declare a variable in C++, you need to specify its data type and give it a
name. Here is the general syntax for declaring a variable:
data_type variable_name;
Here, 'data_type' represents the type of data that the variable will hold, such as int, float, char,
bool, etc. And 'variable_name' is the name given to the variable to uniquely identify it within the
program.
For example, to declare an integer variable called ‘age’, you would use the following code:
int age;
Initializing Variable: After declaring a variable, you can assign a value to it using the
assignment operator (=). For example:
age = 25;
Alternatively, you can declare and initialize a variable in a single line of code, like this:
int age = 25;
This creates a variable called age of type int and initializes it with the value 25.
LAB MANUAL COMPUTER PROGRAMMING

Note that in C++, variables must be declared before they can be used. This means that you
cannot use a variable in your code until you have declared it.
Example:

Activities:
Activity 1.1: What is meant by the following statement?
“Variables must be declared before they can be used.”
Answer: This statement means that we must define a variable, specifying its type and optionally
initializing it, before you use it in any statement or other operations. This declaration tells the
compiler what kind of data the variable will hold and allocates the necessary memory for it.

Activity 1.2: How does the compiler know about the type of the variable?
Answer: The compiler knows a variable's type through its declaration, where you specify the
type and the variable name, optionally initializing it. This informs the compiler about the data
type.
Activity 1.3: Can we change the value of variable?
Answer: Yes, we can change a variable's value after it is declared. Variables are meant to hold
values that can be modified during program execution.
Activity 1.4: Can we change the value of const at the time of declaration? If yes, then
explain when it cannot be changed.
Answer: No we cannot change the value of a const variable after it is declared and initialized.
The const keyword makes a variable constant, meaning its value is fixed and cannot be modified.
Activity 1.5: Declare and initialize all types of variables and show their value on the screen.
Code + Output:
LAB MANUAL COMPUTER PROGRAMMING

Activity 1.6: Differentiate between PascalCase and camelCase, and where should these be
used?
Answer:
PascalCase:
Format: Each word starts with an uppercase letter, including the first word . Eg: PascalCaseExample
Common Uses: Class names, type names, enums, sometimes properties.
camelCase:
Format: The first word starts with a lowercase letter, and each subsequent word starts with an
uppercase letter. Eg: camelCaseExample
Common Uses: Variable names, function names, method names, parameters.
Activity 1.7: Differentiate between float and double datatypes.
Answer:
float: Uses 4 bytes of memory and provides less precision, suitable for simpler calculations.
double: Uses 8 bytes of memory and offers more precision, suitable for more complex and
accurate calculations.
Activity 1.8: Differentiate between int and unsigned int datatypes.
LAB MANUAL COMPUTER PROGRAMMING

Answer:
int: An int can store both negative and positive values. It is used when negative values are
needed.
unsigned int: An unsigned int can store only positive values and zero. allowing for a larger
maximum value compared to int.
Activity 1.9: Differentiate between int and long datatypes.

Answer:

int: An int can store both negative and positive values, typically occupying 4 bytes of memory.
It is used for general integer calculations.

long: A long can also store both negative and positive values and typically occupies 4 bytes on a
32-bit system and 8 bytes on a 64-bit system. It is used for larger integer values.
LAB MANUAL COMPUTER PROGRAMMING

Observations:
In this lab, we learn about different data types and how they can be use including integers,
floating-point numbers, characters, booleans, and void. We validated proficiency in declaring
and initializing variables, understanding the syntax and necessity of data type specification.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 2 DD/MM/YYYY


IDENTIFIERS AND KEYWORDS (RESERVED WORDS)
Lab outcomes:
After completing this lab, students will be able to:
 Understand Identifiers.
 Understand keywords.

Corresponding CLO and PLO:


 CLO-1, PLO-2 (Engineering Knowledge)
Theory:
IDENTIFIERS: Identifiers in C++ are names given to entities such as variables, functions,
classes, and namespaces. These names are used to identify and refer to the entities in the code.
An identifier is a sequence of letters (both uppercase and lowercase), digits, and underscores,
which must begin with a letter or an underscore. Identifiers are case-sensitive, which means that
uppercase and lowercase letters are considered different.
Here are some examples of valid identifiers in C++:
 myVariable
 myFunction
 MyClass
 my_namespace
 var1
 _myVar
 My_Class
Identifiers should be chosen carefully to make the code easy to read and understand. They should
be descriptive and convey the purpose of the entity they are identifying.
KEYWORDS: Keywords are reserved words that have a specific meaning and purpose within the
language. They cannot be used as identifiers (variable names, function names, etc.) and are used
to define the syntax and structure of programs. Here are the definitions of some commonly used
keywords.
Example:
Here are some commonly used keywords in C++ programming language:
LAB MANUAL COMPUTER PROGRAMMING

 auto: used to deduce the data type of a variable automatically


 break: used to exit a loop prematurely
 case: used in a switch statement to define a particular condition to be evaluated
 char: used to define a character data type
 const: used to define a variable as constant, which cannot be modified
 continue: used to skip the current iteration of a loop and move on to the next iteration
 double: used to define a double-precision floating-point data type
 else: used in an if statement to specify a block of code to be executed if the condition is
false
 for: used to create a loop that executes a block of code for a specified number of times
 if: used to test a condition and execute a block of code if the condition is true
 int: used to define an integer data type
 namespace: used to define a scope of identifiers
 return: used to return a value from a function
 static: used to define a variable or function as static, which means it retains its value
between function calls
 struct: used to define a user-defined data type
 switch: used to create a multiple-branch statement
 void: used to indicate that a function does not return any value
 while: used to create a loop that executes a block of code as long as the specified
condition is true.

Activities:
Activity 2.1: Write a program in C++ that will declare two variables of integer type and
assign value to them.
LAB MANUAL COMPUTER PROGRAMMING

Code +Output:

Activity 2.2: Declare a variable with identifier that will contain _ symbol only.
Code+Output:

Activity 2.3: Is the following identifier valid for variable, if yes, then explain why:
decimal @abstract;
Code:
It is a Invalid Character (@) it should be (_)or a-z alphabet. Because special character is not
aloud
Output:
Error.
Activity 2.4: Is the following identifier valid, if not explain why:
long my data;
int price$;
LAB MANUAL COMPUTER PROGRAMMING

Answer: my data is invalid because it contains a space. Identifiers in C++ cannot contain
whitespace. price$ is invalid because it contains a dollar sign $, which is not allowed in C++
identifiers. Valid characters are letters, digits, and underscores.

Observations:
In this lab, we have gained a clear understanding of identifiers and keywords in C++. We have
learned the rules for creating valid identifiers and the importance of choosing descriptive names
to enhance code readability. Additionally, we have recognized the significance of keywords in
defining the syntax and structure of C++ programs.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 3 DD/MM/YYYY


OPERATORS IN C++
Lab outcomes:
After completing this lab, students will be able to:
 Implement the arithmetic operators.
 Implement the unary operators.
 Implement the relational operators.
 Implement the logical operators.
 Implement the assignment operators.

Corresponding CLO and PLO:


 CLO-1, PLO-2 (Engineering Knowledge)
Theory:
C++ is a programming language that provides a variety of operators to perform different types of
operations on variables and values. These operators can be broadly categorized into several
types, including arithmetic, unary, relational, logical, and assignment operators. Here's a brief
description of each:
1. Arithmetic Operators: These operators are used to perform mathematical calculations
on numerical values. The arithmetic operators in C++ include:
 Addition (+): Adds two values together.
 Subtraction (-): Subtracts one value from another.
 Multiplication (*): Multiplies two values together.
 Division (/): Divides one value by another.
 Modulus (%): Finds the remainder when one value is divided by another.
2. Unary Operators: These operators are used to perform operations on a single operand.
The unary operators in C++ include:
 Increment (++): Increases the value of a variable by one.
 Decrement (--): Decreases the value of a variable by one.
 Negation (-): Changes the sign of a numerical value.
 Logical NOT (!): Inverts the logical value of a boolean expression.
LAB MANUAL COMPUTER PROGRAMMING

3. Relational Operators: These operators are used to compare two values and return a
boolean result. The relational operators in C++ include:
 Equal to (==): Checks if two values are equal.
 Not equal to (!=): Checks if two values are not equal.
 Greater than (>): Checks if one value is greater than another.
 Less than (<): Checks if one value is less than another.
 Greater than or equal to (>=): Checks if one value is greater than or equal to
another.
 Less than or equal to (<=): Checks if one value is less than or equal to another.
4. Logical Operators: These operators are used to combine boolean expressions and return
a boolean result. The logical operators in C++ include:
 Logical AND (&&): Returns true if both boolean expressions are true.
 Logical OR (||): Returns true if at least one boolean expression is true.
 Logical NOT (!): Inverts the logical value of a boolean expression.
5. Assignment Operators: These operators are used to assign a value to a variable. The
assignment operators in C++ include:
 Simple assignment (=): Assigns a value to a variable.
 Addition assignment (+=): Adds a value to a variable and assigns the result to the
variable.
 Subtraction assignment (-=): Subtracts a value from a variable and assigns the result
to the variable.
 Multiplication assignment (*=): Multiplies a variable by a value and assigns the
result to the variable.
 Division assignment (/=): Divides a variable by a value and assigns the result to the
variable.
 Modulus assignment (%=): Computes the remainder of a variable divided by a
value and assigns the result to the variable.

Activities:
Activity 3.1: Write a program in C++ that takes two numbers as input and print their sum,
difference, product, and quotient using the arithmetic operators.
Code+Output:
LAB MANUAL COMPUTER PROGRAMMING

Activity 3.2: Write a program in C++ that takes an integer as input and print its square
and cube using the multiplication operator.
Code+Output:

Activity 3.3: Write a program in C++ that takes an integer as input and prints its value
after incrementing and decrementing it using the unary operators.
Code+ Output:
LAB MANUAL COMPUTER PROGRAMMING

Activity 3.4: Write a program in C++ that takes two numbers as input and prints whether
the first number is greater than, less than, or equal to the second number using the
relational operators.
Code+Output:

Activity 3.5: Write a program in C++ that takes a character as input and print whether it is
a vowel or a consonant using the logical operators.
Code+output:
LAB MANUAL COMPUTER PROGRAMMING

Activity 3.6: Write a program in C++ that takes an integer as input and compute its
factorial using the multiplication and assignment operators.
Code+output:

Observations:
In this lab, we about operators which provided us with essential tools for manipulating data
effectively within programs. Understanding arithmetic, unary, relational, logical, and assignment
operators enhances our problem-solving skills. Basically this operator equips us to write brief
and efficient code, advancing our proficiency in C++ programming.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 4 DD/MM/YYYY


CONDITIONAL STATEMENTS (if, else, else if)
Lab outcomes:
After completing this lab, students will be able to:
 Implement if, else, and else if statements in C++.

Corresponding CLO and PLO:


 CLO-2, PLO-3 (Design and Development of Solutions)
Theory:
Conditional statements are used in programming to make decisions based on certain conditions.
In C++, the most commonly used conditional statements are if, else, and else if.
The if statement is used to execute a block of code if a certain condition is true. The basic syntax
of the if statement in C++ is as follows:
if (condition) {
// code to be executed if condition is true
}
The condition is an expression that evaluates to either true or false. If the condition is true, the
code inside the curly braces will be executed.
The else statement is used in conjunction with the if statement to execute a block of code if the
condition in the if statement is false. The basic syntax of the if-else statement in C++ is as
follows:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
If the condition in the if statement is false, the code inside the else block will be executed.
The else if statement is used to test multiple conditions. It is used when there are multiple
possible outcomes based on different conditions. The basic syntax of the if-else if statement in
C++ is as follows:
LAB MANUAL COMPUTER PROGRAMMING

if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
If condition1 is true, the code inside the first if block will be executed. If condition1 is false and
condition2 is true, the code inside the else if block will be executed. If both conditions are false,
the code inside the else block will be executed.

Activities:
Activity 4.1: Write a program in C++ that prompts the user to enter a password, and
checks if the password meets certain criteria (e.g., contains at least one uppercase letter,
one lowercase letter, and one number). Use if statements to check each criterion, and print
a message indicating whether the password is valid or not.
Code+ Output:

Activity 4.2: Write a program in C++ that prompts the user to enter a temperature in
Celsius or Fahrenheit, and converts it to the other scale. Use an if-else statement to
determine which conversion formula to use based on the user's input.
Code:
LAB MANUAL COMPUTER PROGRAMMING

Output:

Observations:
In this lab, we learned to use if, else, and else if statements in C++ to control program flow based
on conditions. If statements run code when a condition is true, if-else statements choose between
two paths, and else if statements handle multiple conditions. These skills help us make decisions
in programs, improving our problem-solving
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 5 DD/MM/YYYY


SWITCH STATEMENTS
Lab outcomes:
After completing this lab, students will be able to:
 Implement the switch statements.

Corresponding CLO and PLO:


 CLO-2, PLO-3 (Design and Development of Solutions)
Theory:
In C++, a switch statement is a flow control statement that allows a program to evaluate an
expression and then execute different statements based on the value of that expression. It is
commonly used as an alternative to a series of nested if statements when you need to test a single
variable against multiple possible values.
The basic syntax of a switch statement in C++ is as follows:
switch(expression) {
case value1:
// code to execute if expression equals value1
break;
case value2:
// code to execute if expression equals value2
break;
...
default:
// code to execute if expression does not equal any of
the specified values
break;
}
Here, the expression is the variable or value that is being tested, and the case statements provide
the different possible values that the expression might have. If the expression matches one of the
LAB MANUAL COMPUTER PROGRAMMING

specified values, the code inside the corresponding case block is executed. The break statement
is used to exit the switch statement after a match is found.
The default case is optional and is used to provide a default action to take if none of the
specified case values match the expression. If no break statement is included after the default
case, the program will continue to execute the code in the subsequent case blocks until it
encounters a break statement or the end of the switch statement.

Activities:
Activity 5.1: Write a program in C++ that simulates a vending machine, allowing users to
select items and pay with different forms of currency. Use a switch statement to determine
which item the user selected and how much money they inserted.
Code+ Output:

Activity 5.2: Write a program in C++ that allows users to enter two numbers and an
operation (+, -, *, /) and displays the result. Use a switch statement to determine which
operation to perform.
LAB MANUAL COMPUTER PROGRAMMING

Code:

Output:

Observations:
In this lab, we learned to use switch statements in C++ to control program flow based on an
expression's value. Switch statements provide an efficient alternative to use multiple if
statements, making code more organized and readable.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 6 DD/MM/YYYY

RESTAURANT BILL CALCULATOR (Open-Ended Lab)


Lab outcomes:
After completing this lab, students will be able to:
 Create a program that calculates the cost of a meal at a restaurant, including tax and tip.

Corresponding CLO and PLO:


 CLO-2, PLO-3 (Design and Development of Solutions)
Problem Statement:
Write a C++ program that calculates the total cost of a meal at a restaurant, including tax and tip.
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.

Observations:
Please write your observation after conducting this lab, you have to write in few lines, what did
you learn in this lab.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Not Student is Student fully Student can Student has Student has
Submitted unable to understand lab understand the implemented the completed all lab
identify the objectives and requirements and correct solution objectives with
Open Ended given adapted correct propose correct and got favorable flawless results
Laboratory laboratory approach to solution without results and drawn
objectives provide solution results appropriate
conclusion

Category Ungraded Very Poor Poor Fair Good Excellent


Marks 0.0 0.5 1.0 1.5 2.5 3.0
Date Total Marks Instructor’s Signature
Report not Plagiarized Requirements are Observations are Appropriate Correctly drawn
Open Ended submitted content listed and recorded along computations or conclusion with
Laboratory presented or experimental with detailed numerical exact results and
Report incomplete procedure is procedure analysis is complete report
submission presented performed in all respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0.0 0.3 0.6 1.0 1.5 2.0
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 7 DD/MM/YYYY


FOR LOOPS IN C++
Lab outcomes:
After completing this lab, students will be able to:
 Implement the FOR loop in C++.

Corresponding CLO and PLO:


 CLO-2, PLO-3 (Design and Development of Solutions)
 CLO-3, PLO-8 (Ethics)
Theory:
A for loop is a control structure in C++ used for iterating or repeating a set of statements a
certain number of times. The general syntax of a for loop in C++ is as follows:
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
The initialization section is used to initialize the loop counter variable to a starting value. The
condition section is used to check if the loop counter variable has reached a certain condition,
and if it is true, the loop continues to run. The increment/decrement section is used to update
the loop counter variable after each iteration.
Here's an example of a for loop that prints the numbers 1 to 10:
for (int i = 1; i <= 10; i++) {
cout << i << endl;
}
In this example, i is initialized to 1, the condition checks if i is less than or equal to 10, and i is
incremented by 1 after each iteration. The loop will run 10 times, printing the numbers 1 to 10 to
the console.

Activities:
Activity 7.1: Write a C++ program that asks the user to input a number, and then prints
the multiplication table for that number using a for loop.
Code:
LAB MANUAL COMPUTER PROGRAMMING

Please write or paste your code snap here.


Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.
Activity 7.2: Write a program in C++ that asks the user to enter a number N, and then
calculates the sum of the first N natural numbers using a for loop.
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.

Observations:
Please write your observation after conducting this lab, you have to write in few lines, what did
you learn in this lab.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING

ETHICS

Category Very Poor Fair Excellent Obtained Marks

Absent 15 minutes late On Time 40% of the Total


Punctuality Assigned Marks

Assigned Marks 0 2 4
Obtained Marks /4
Disrespectful behavior Loud and disturbing Good and 30% of the Total
with instructor and / or behavior cooperating behavior Assigned Marks
Behavior with class fellows
OR
Absent
Assigned Marks 0 1.5 3
Obtained Marks /3
Leaves chairs in unorderly Put chair in proper 30% of the Total
manner, doesn’t turn off place, turns off tool / Assigned Marks
tool / software / equipment software / equipment
Regard of Equipment
after lab after lab
OR
Absent
Assigned Marks 0 3

Obtained Marks /3

Instructor’s Signature Total Marks /10


Obtained
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 8 DD/MM/YYYY


WHILE LOOPS IN C++
Lab outcomes:
After completing this lab, students will be able to:
 Implement the WHILE loop.

Corresponding CLO and PLO:


 CLO-2, PLO-3 (Design and Development of Solutions)
 CLO-3, PLO-8 (Ethics)
Theory:
In C++, a while loop is a control flow statement that repeatedly executes a block of code as long
as a specified condition is true. The syntax of a while loop in C++ is as follows:
while (condition) {
// code to be executed while the condition is true
}
Here, condition is an expression that is evaluated before each iteration of the loop. If condition
is true, then the code inside the curly braces {} is executed, and then the condition is checked
again. This process continues until the condition becomes false, at which point control passes to
the code immediately following the while loop.
For example, the following code uses a while loop to print the numbers from 1 to 10:
#include <iostream>

int main() {
int i = 1;

while (i <= 10) {


std::cout << i << " ";
i++;
}

return 0;
}
LAB MANUAL COMPUTER PROGRAMMING

In this example, the while loop continues to execute as long as i is less than or equal to 10. Inside
the loop, the current value of i is printed to the console, and then i is incremented by 1. This
process repeats until i reaches 11, at which point the loop terminates and the program ends.

Activities:
Activity 8.1: Write a program in C++ that prompts the user to enter a password, and then
uses a while loop to keep asking for the password until the user enters a correct password
(e.g., "password123"). Print a message indicating that the user has entered the correct
password.
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.
Activity 8.2: Write a program in C++ that prompts the user to enter a series of numbers,
and then uses a while loop to sum up the numbers and print the result to the console.
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.

Observations:
Please write your observation after conducting this lab, you have to write in few lines, what did
you learn in this lab.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING

ETHICS

Category Very Poor Fair Excellent Obtained Marks

Absent 15 minutes late On Time 40% of the Total


Punctuality Assigned Marks

Assigned Marks 0 2 4
Obtained Marks /4
Disrespectful behavior Loud and disturbing Good and 30% of the Total
with instructor and / or behavior cooperating behavior Assigned Marks
Behavior with class fellows
OR
Absent
Assigned Marks 0 1.5 3
Obtained Marks /3
Leaves chairs in unorderly Put chair in proper 30% of the Total
manner, doesn’t turn off place, turns off tool / Assigned Marks
tool / software / equipment software / equipment
Regard of Equipment
after lab after lab
OR
Absent
Assigned Marks 0 3

Obtained Marks /3

Instructor’s Signature Total Marks /10


Obtained
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 9 DD/MM/YYYY


DO WHILE LOOPS IN C++
Lab outcomes:
After completing this lab, students will be able to:
 Implement the DO WHILE loop in C++.

Corresponding CLO and PLO:


 CLO-2, PLO-3 (Design and Development of Solutions)
 CLO-3, PLO-8 (Ethics)
Theory:
A do-while loop in C++ is a type of loop structure that executes a block of code repeatedly until
a specified condition is met. Unlike the while loop, the do-while loop executes the block of code
at least once, even if the condition is false.
The general syntax of a do-while loop in C++ is as follows:
do {
// block of code to be executed
} while (condition);
In this syntax, the block of code is executed first, and then the condition is evaluated. If the
condition is true, the loop continues to execute, repeating the block of code again. If the
condition is false, the loop terminates, and the program continues with the next statement after
the loop.
Here is an example of a do-while loop in C++ that prompts the user to enter a number and
continues to prompt until a valid number is entered:
#include <iostream>
using namespace std;

int main() {
int num;

do {
cout << "Enter a number between 1 and 10: ";
cin >> num;
} while (num < 1 || num > 10);
LAB MANUAL COMPUTER PROGRAMMING

cout << "You entered: " << num << endl;


return 0;
}

In this example, the block of code prompts the user to enter a number, and the condition checks
if the number is between 1 and 10. If the number is not within that range, the loop continues to
prompt the user to enter a number until a valid one is entered.

Activities:
Activity 9.1: Write a program in C++ that prompts the user to enter a series of numbers.
Use a do-while loop to repeatedly prompt the user to enter a number until they indicate
that they are done. Then, calculate the average of the numbers entered.
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.
Activity 9.2: Write a program in C++ that prompts the user to enter a number n and then
prints the first n numbers of the Fibonacci sequence. Use a do-while loop to repeatedly
calculate and print the next number in the sequence until n numbers have been printed.
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.

Observations:
Please write your observation after conducting this lab, you have to write in few lines, what did
you learn in this lab.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING

ETHICS

Category Very Poor Fair Excellent Obtained Marks

Absent 15 minutes late On Time 40% of the Total


Punctuality Assigned Marks

Assigned Marks 0 2 4
Obtained Marks /4
Disrespectful behavior Loud and disturbing Good and 30% of the Total
with instructor and / or behavior cooperating behavior Assigned Marks
Behavior with class fellows
OR
Absent
Assigned Marks 0 1.5 3
Obtained Marks /3
Leaves chairs in unorderly Put chair in proper 30% of the Total
manner, doesn’t turn off place, turns off tool / Assigned Marks
tool / software / equipment software / equipment
Regard of Equipment
after lab after lab
OR
Absent
Assigned Marks 0 3

Obtained Marks /3

Instructor’s Signature Total Marks /10


Obtained
LAB MANUAL COMPUTER PROGRAMMING

LAB NO. 10 DD/MM/YYYY


NESTED LOOPS IN C++
Lab outcomes:
After completing this lab, students will be able to:
 Implement the nested loops in C++.

Corresponding CLO and PLO:


 CLO-2, PLO-3 (Design and Development of Solutions)
 CLO-3, PLO-8 (Ethics)
Theory:
In C++, a nested loop is a loop statement that is inside another loop statement. This means that
the inner loop will repeat its block of code multiple times for every iteration of the outer loop.
There are three types of loops in C++: for, while, and do-while. Each of these loop types can be
nested inside another loop type to create a nested loop structure.
Here's an example of a nested for loop in C++:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << i << "," << j << " ";
}
cout << endl;
}
In this example, there are two for loops nested inside each other. The outer loop iterates from i =
0 to i < 5, and the inner loop iterates from j = 0 to j < 5. The inner loop is executed five times for
each iteration of the outer loop, resulting in a total of 25 iterations.
Similarly, we can create nested while or do-while loops by placing one loop inside the other. The
syntax is similar to that of nested for loops, with the loop condition and body being defined
within the braces of the outer loop.

Activities:
Activity 10.1: Write a C++ program to take an integer input from the user and then create
a pyramid shape with that many levels. For example, if the user inputs 4, the program
should create a pyramid shape with 4 levels like this:
LAB MANUAL COMPUTER PROGRAMMING

*
***
*****
*******

Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.
Activity 10.2: Write a C++ program that takes an integer input from the user and creates a
square pattern using the letters of the alphabet. For example, if the user inputs 4, the
program should create the following pattern:
ABCD
EFGH
IJKL
MNOP
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.

Observations:
Please write your observation after conducting this lab, you have to write in few lines, what did
you learn in this lab.
LAB MANUAL COMPUTER PROGRAMMING

Rubrics
Report not Plagiarized Requirements Observations Appropriate Correctly
submitted content are listed and are recorded computation drawn
presented or experimental along with s or conclusion
Laboratory incomplete procedure is detailed numerical with
Reports submission or presented procedure analysis is exact results
late performed and complete
report in all
respects
Category Ungraded Very Poor Poor Fair Good Excellent
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature

Absent Student failed Student Student Student Student


to develop a developed a developed a developed a excelled in
functional partially mostly fully developing
code or functional functional functional fully
complete the code and code and met code and functional
lab objective struggled to the lab met the lab code,
within the accomplish objective objective exceeding
simulation the lab within the within the expected
environment. objective simulation simulation results within
They exhibit within the environment, environment simulation
inadequate simulation achieving , achieving environment.
Demonstrat understanding environment. satisfactory expected They
ion in configuring, They results. They results. They demonstrate
operating, and demonstrate exhibit demonstrate mastery in
executing the minimal moderate proficiency independently
simulation understanding proficiency in in configuring,
independently. in configuring, configuring, configuring, operating and
operating, and operating, and operating, executing the
executing the executing the and simulation.
simulation simulation executing
independently. independently. the
simulation
independentl
y.
Category Ungraded Very Poor Poor Fair Good Excellent

Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING

ETHICS

Category Very Poor Fair Excellent Obtained Marks

Absent 15 minutes late On Time 40% of the Total


Punctuality Assigned Marks

Assigned Marks 0 2 4
Obtained Marks /4
Disrespectful behavior Loud and disturbing Good and 30% of the Total
with instructor and / or behavior cooperating behavior Assigned Marks
Behavior with class fellows
OR
Absent
Assigned Marks 0 1.5 3
Obtained Marks /3
Leaves chairs in unorderly Put chair in proper 30% of the Total
manner, doesn’t turn off place, turns off tool / Assigned Marks
tool / software / equipment software / equipment
Regard of Equipment
after lab after lab
OR
Absent
Assigned Marks 0 3

Obtained Marks /3

Instructor’s Signature Total Marks /10


Obtained

You might also like