You are on page 1of 4

Page 1 of 4

NAME DATE REMARKS

CAGAYAN, JUAN CARLOS S. September 20. 2023

COURSE/YEAR/SECTION INSTRUCTOR

CPE 1-A MRS. HILDA W. SANTOS

Activity No. 4
Using Variables and Constants

Learning Outcomes getch();


After completing this activity, you will be able }
to:
1. allocate memory with both variables and What is the result/output? 5
named constants;
2. assign values to variables when
expressions are evaluated;
3. assign values to variables through input;
and,
4. identify and use the string data type. In C++, memory locations that can be changed
are called variables. You declare variables with
Introduction the following syntax:
Data must be loaded into main memory before it
datatype identifier;
can be manipulated. Storing data into the PC’s
memory is a two-step process:
Data is stored in variables either through an
1. instruct the PC to allocate memory assignment statement or through an input
2. include statements into the program to put statement. Assignment statements use the =
data into the allocated memory operator.

When you allocate memory, you use a unique Try running the following code:
name to identify each memory location.
#include <iostream.h>
Additionally, you indicate the type of data to be #include <conio.h>
stored. A memory location whose value cannot void main()
be changed is called a named constant and has {
the following syntax: int i=10;
i=i+1;
const datatype identifier = value cout<<i<<endl;
getch();
Try running the following code: }

#include <iostream.h>
#include <conio.h>
void main()
{
const int i=5;
cout<<i<<endl;
What is the result/output? 11
Page 2 of 4

#include <string>
void main()
{
string str1;
str1="Hello World!";
cout<<str1<<endl;
getch();
}
Data that is received from the keyboard can be
put into variables using cin and the stream What is the result/output? Hello World!
extraction operator >>.

Try running the following code:

#include <iostream.h>
#include <conio.h>
void main()
{
char c;
cin>>c; Note: We have to use <string> in our program
getch(); code and not <string.h>.
}
Summary
What is the result/output? Data is stored onto main memory
In its current state, blank as this code will pause programmatically using either named constants
its execution, expecting the user to input a or variables. They must be unique throughout
character. Once a character is provided, the the program and can be manipulated using
program will conclude, and the outcome will be various operators.
determined by the specific character entered by
the user. Exercises
1. Evaluate the value of the final expression:
int a, b=9;

a=b;

int d=2, x=3, y=1;

y=4+d-x;

int number=10;
The data type string is a programmer-defined
data type. A string is a sequence of zero or more number=number+5;
characters enclosed in double quotation marks.
double val=10.0;
Every character has a relative position in the
string (including spaces). The length of a string val=val+0.5;
is the number of characters in it. C++ provides
const int b=9;
the library string to process strings
effectively. double def=123.5;

int y;
#include <iostream.h>
#include <conio.h>
Page 3 of 4

y=def;

int num1=8,num2=3,temp;

temp=num1;

num1=num2;

double x=5.0;

int y=4;

x=x+y/4;

double x=5.0;

int y=4;

y=x+y/4;

int count=0;

cin>>count;

//assume the user


//enters 8

2. Write a C++ program that will allow the


user to enter two numbers and displays the
sum and difference.

3. Write a C++ program that will allow the


user to enter the value of side of square and
compute the following:
Total area = 6a2
Volume = a3
Perimeter = sum of all sides

Conclusion

4. Write a C++ program that will allow the


user to enter the binary numbers (maximum
of eight bits) and convert it into decimal
equivalent.
Page 4 of 4

References
https://www.w3schools.com/cpp/
cpp_variables.asp
https://www.learn-cpp.org/en/
Variables_and_Types

You might also like