Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

LAB # 2

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.

Naming Variables – Identifiers


The “variable name” is termed as “identifier” in programming language.
Conventions for Variable naming
• Valid characters for variable naming.
1. Upper case (A-Z) or lower case alphabets (a-z).
2. Numeric digits (0-9).
3. Underscore character.
• All other character such as !@#$%^&)|~ even white/blank spaces are not allowed.
• An identifier can begin with an alphabet or underscore. A digit is not allowed in the beginning.

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.

Integer int value; Identifier


type

NOTE: - a variable declaration always ends with a semicolon (;).

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

Adds two operands


Add + 2 k=8
and returns the result

Subtracts the 2nd


Subtract - 2 k=3
operand from the 1st

Multiplies two
k=
Multiply * 2 operands and returns
20.4
the result

Divides the 1st


operand by the 2nd
Divide / 2 k = 5.1
operand and returns
the result

Divides the 1st


operand by the 2nd
Remainder % 2 k=1
operand and returns
the remainder

increases the value a = 11,


of operand by 1 in c = 11,
increment ++ 1
prefix or postfix b = 4,
manner d=3

decreases the value a = 9,


of operand by 1 in c = 9,
decrement -- 1
prefix or postfix b = 2,
manner d=3
Taking Input from User
Following program uses the function cin from the <iostream> library to take two numbers from user, calculates the sum
and displays it on {screen}

Listing 1: Input
# include <iostream >
using namespace std;

// A program which calculates the sum of 2 integer.


// The two integers are user defined.

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

Q#5: For a triangle with following sides, base = 3, perpendicular = 4 and


hypotenuse = 5, Prove this triangle is a right angled one.
Q#6: Write a C++ program to calculate the area of a triangle. Ask the user for the
values of base and altitude.
Q#7: Write a C++ program to calculate the volume of a sphere. Take the value of
radius from the user.
Q#8: Take a length in centimeter and convert it into meters and kilometers.
Q#9: Write a simple calculator program in C++ which takes 2 values from the user
and performs addition, subtraction, division, multiplication on them.
Q#10: Two Resistances of 120 and 1k ohms are added in series and parallel.
Calculate their equivalent resistances for both cases.
Q#11: Write a C++ program that takes value of temperature (C0) from the user and
converts it into Fahrenheit. (F0=1.8*C0 +32)
Q#12: Write a C++ program that takes a 3 digit number from user and separate its
digits into tens, hundreds, units etc.

You might also like