Lab 1 To Lab 3 Complete
Lab 1 To Lab 3 Complete
Lab 1 To Lab 3 Complete
Computer Programming
Submitted by
Muhammad Hamza | 62908
Lab Instructor
Session
Spring – 2024
LAB MANUAL COMPUTER PROGRAMMING
CERTIFICATE
Session Fall 2024, under the supervision of his or her lab instructor, Engr.
Rehmat Ullah.
_________________ __________________
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
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
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
LAB MANUAL COMPUTER PROGRAMMING
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
LAB MANUAL COMPUTER PROGRAMMING
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
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
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
ETHICS
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
int main() {
int i = 1;
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
ETHICS
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
int main() {
int num;
do {
cout << "Enter a number between 1 and 10: ";
cin >> num;
} while (num < 1 || num > 10);
LAB MANUAL COMPUTER PROGRAMMING
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
ETHICS
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
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
Marks 0 1 2 3 4 5
Date Total Marks Instructor’s Signature
LAB MANUAL COMPUTER PROGRAMMING
ETHICS
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