0% found this document useful (0 votes)
4 views4 pages

Lab #2

Lab No. 2 focuses on primitive data types and variables in C++, teaching students to understand and implement these concepts. The lab covers five primitive data types: integers, floating-point numbers, characters, booleans, and void, along with their declaration and initialization. Activities are included to reinforce learning, such as declaring variables, understanding variable types, and differentiating between data types.

Uploaded by

khansab031020
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)
4 views4 pages

Lab #2

Lab No. 2 focuses on primitive data types and variables in C++, teaching students to understand and implement these concepts. The lab covers five primitive data types: integers, floating-point numbers, characters, booleans, and void, along with their declaration and initialization. Activities are included to reinforce learning, such as declaring variables, understanding variable types, and differentiating between data types.

Uploaded by

khansab031020
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
You are on page 1/ 4

LAB COMPUTER PROGRAMMING

MANUAL

LAB NO. 2
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-1 (Engineering Knowledge)

Lab Summary:
In this Lab students were introduced to primitive data types and variables,
along with the concepts of declaration, initialization, and syntax structure.
The lab emphasized the importance of using an IDE for writing and compiling
code effectively.

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
LAB COMPUTER PROGRAMMING
MANUAL

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.
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.
LAB COMPUTER PROGRAMMING
MANUAL

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.
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: Please write your answer here.
Activity 1.2: How does the compiler know about the type of the variable?
LAB COMPUTER PROGRAMMING
MANUAL

Answer: Please write your answer here.


Activity 1.3: Can we change the value of variable?
Answer: Please write your answer here.
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: Please write your answer here.
Activity 1.5: Declare and initialize all types of variables and show their value on the screen.
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 1.6: Differentiate between PascalCase and camelCase, and where should these be
used?
Answer: Please write your answer here.
Activity 1.7: Differentiate between float and double datatypes.
Answer: Please write your answer here.
Activity 1.8: Differentiate between int and unsigned int datatypes.
Answer: Please write your answer here.
Activity 1.9: Differentiate between int and long datatypes.
Answer: Please write your answer here.
Observations:
Please write your observation after conducting this lab, you must write in few lines, what did you
learn in this lab. Please include 4-5 lines here.

You might also like