Aula Programasaun C++
Introduction to Variables, Data Types, and Simple Calculations in C++
Module Objectives: By the end of this module, students will be able to:
1. Understand what a variable is and its purpose in programming.
2. Identify and use basic C++ data types: int, float, char, and string.
3. Write simple C++ programs to perform calculations.
4. Understand the concepts of algorithm and flowchart for program planning.
a. Introduction
C++ is a programming language used for creating software, simulations, and embedded
systems.
In this module, students will learn basic concepts using a simple program to calculate the
volume of a rectangular block.
b. Variables in C++
• Definition: A variable is a storage location in memory that holds data.
• Components of a Variable:
o Name → identifier for the variable
o Type → what kind of data it can store
o Value → actual data stored
Example:
int age; // 'int' is the data type, 'age' is the variable
c. Basic Data Types
Data Type Description Example
int Stores integer numbers 10, 25, 100
float Stores decimal numbers 3.5, 7.25
char Stores a single character 'A', 'B', 'C'
string Stores text or words "Hello", "Programming"
d. Example: Using Different Data Types
#include <iostream>
using namespace std;
int main() {
int age = 20;
float height = 170.5;
char grade = 'A';
string name = "Carlos";
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Height: " << height << endl;
cout << "Grade : " << grade << endl;
return 0;
}
Output:
Name : Carlos
Age : 20
Height: 170.5
Grade : A
e. Algorithm
Before writing code, we plan using an algorithm:
1. Start
2. Declare variables: length, width, height, result
3. Input length
4. Input width
5. Input height
6. Calculate result = length × width × height
7. Display result
8. End
f. Flowchart
The flowchart visually represents the algorithm:
g. C++ Program Example (Volume Calculation)
#include <iostream>
using namespace std;
int main() {
// Variable declaration
float length, width, height, result;
cout << "Program to Calculate Volume (Length x Width x Height)" <<
endl;
cout << "Enter Length : ";
cin >> length;
cout << "Enter Width : ";
cin >> width;
cout << "Enter Height : ";
cin >> height;
// Calculation
result = length * width * height;
cout << "Volume = " << result << endl;
return 0;
}
Example Input/Output:
Enter Length : 5
Enter Width : 4
Enter Height : 3
Volume = 60
Program 2
#include <iostream>
using namespace std;
int main() {
// Deklarasaun Variabel Sira
float Naruk; // Rai valor Naruk
float Luan; // Rai valor Luan
float Aas; // Rai valor Aas
float Rezultadu; // Rai rezultadu Kalkulasaun
// Input dadus husi Utilizador
cout << "Program Sura Volume (Naruk x Luan x A'as)" << endl;
cout << "Valor Naruk : ";
cin >> Naruk;
cout << "Valor Luan : ";
cin >> Luan;
cout << "Valor Aas : ";
cin >> Aas;
// Prosesu kalkulasaun
Rezultadu = Naruk * Luan * Aas;
// Output
cout << "Rezultadu kalkulasaun = " << Rezultadu << endl;
return 0;
}
8. Resume
Variables are containers to store data.
int → integers, float → decimals, char → single character, string → text.
Plan your program using algorithms and flowcharts before coding.
Simple C++ programs can take input, process calculations, and display output.