Professional Documents
Culture Documents
Lab 02manual
Lab 02manual
Recap
Comments
Line comment: Block type comment:
#include <iostream> /*
// the entry point of the program Ex2_01.cpp
int main() A Simple Program Example
{ */
std::cout << "Hello, world!"; #include <iostream>
} using namespace std;
int main()
{
cout << “Hello, BS 19/23.!\n”;
getchar();
return 0;
}
Variables
A primary objective in all computer programs is to manipulate/work on some data and get some answers. An
essential element in this process is having a piece of memory that you can call your own, where you can store an item
of data. Each individual piece of memory so specified is called a variable.
Examples
Good Variable name Bad/Invalid variable name
• price • 8_ball
• discount • 7_up
• pShape • 6-pack
• value_ • Hash!
• COUNT • Mary-Ann
• Num_1
Keywords in C++
There are reserved words in C++ called keywords that have special significance within the language. Remember the
keywords in C++ are case sensitive. E.g. For example, the program that you entered earlier in the chapter contained the
keywords int and return; if you write Int or Return, these are not keywords and, therefore, will not be recognized as such.
Variables declaration
Variable declaration is a program statement that specifies the name/identifier of a variable of a given type.
Variable Initialization
Giving each of the variables an initial value. There are two methods to do so.
Standard Notation Functional Notation
int value = 0; int value(0);
int count = 10; int count(10);
int number = 5; int number(5);
Fundamental Datatypes
Type Size in bytes Range of Values Examples
int toeCount = 10;
int 4 -2,147,483,648 to 2,147,483,647
int num_1 = 23;
±3.4*10^±38 with approximately 7-digits float pi = 3.14159f;
float 4
accuracy float e = 2.718;
±1.7 *10^±308 with approximately 15- double length = 25.4;
double 8
digits accuracy double width = 12.45;
char letter = 'A';
char 1 -128 to 127
char letter ('C');
bool colorIsRed = true;
bool 1 true or false
bool test_result = false;
Arithmetic Operators:
# of
Name Symbol Description Example Result
operands
Multiplies two
k=
Multiply * 2 operands and returns
20.4
the result
Listing 1: Input
# include <iostream >
using namespace std;
int main ()
{
int x;
int y;
cout << “Enter 1st integer \n”;
cin >> x;
cout << “Enter 2nd integer \n”;
cin >> y;
int sum = x+y;
cout << "Sum of numbers is " << sum << endl ;
return 0;
}
Q#1: Write a C++ program to find the area & perimeter of a circle. Take required
value from the user (radius).
Q#2: Write a C++ program which shows that arc length of a circle of given radius =
5cm subtended by an angle of 900 is equal to 1/4th of its perimeter.
Q#3: Write a C++ program which converts km/h to miles/hr. Note: 1km/hr =
1.60934 mile/hr.
Q#4: Write a C++ program to print the following pattern.
xxxx
x x
x x x
x xxxxx xxxxx
x x x
x x
xxxx