PROGRAMMING FUNDAMENTALS Lab Manual SESSION: 2022 SEMESTER: 1ST Course Code: CC1021L LAB 02: Variables, Datatypes and Operators CLO 2 Objective(s): 1. Using standard datatypes in C++ 2. Declaring and Initialization of Variables 3. Declaring and Defining Constants 4. Use of Arithmetic Operators 5. Use of manipulator functions Data types: Data types portray the amount of memory to be allocated. Most common data types are integer, floating- point, character, strings, bool. Built – In Data Types in C++ DATA TYPE bool MEMORY (BYTES) 1 char unsigned char short unsigned short int unsigned int long unsigned long float double long double 1 1 2 2 2 2 4 4 4 8 10 MINIMUM VALUE Logical Value true/false -128 0 -32768 0 -32768 0 -2147483648 0 10-38 10-308 104932 MAXIMUM VALUE Logical Value true/false 127 255 32767 65535 32767 65535 2147483647 4294967295 1038 10308 104932 Declaration of Variables Declaration of variables means that compiler assigns a memory location for the variable. Declaration of a variable consists of data type and the variable name. Data type var_name; e.g: int var,number; float num sum; char ch; Initialization of variables Initialization of variables assigns an initial value to the variable declared. Variable value is initialized by giving variable a name consisting of equal sign followed by a constant value. Data type var_name=value; e.g: int num=5; float num=4.5, sum=0; char ch=’A’; Example : // This program has variables of several of the integer types. #include <iostream> using namespace std; int main() { int checking; unsigned int miles; long days; checking = -20; miles = 4276; days = 189000; cout << "We have made a long journey of " << miles; cout << " miles.\n"; cout << "Our checking account balance is " << checking; cout << "\nAbout " << days << " days ago Columbus "; cout << "stood on this spot.\n"; return 0; } Program Output: We have made a long journey of 4276 miles. Our checking account balance is -20 About 189000 days ago Columbus stood on this spot. OPERATORS Operators are in C++ to perform operations on variables and constants. Operators in C++ are: • Arithmetic Operators • Relation Operators • Logical Operators • Assignment Operators • Compound Operators 1) Arithmetic Operator Basic arithmetic operators in C++ are: *, /, -, +, ++, -- are the arithmetic operators. Example Suppose X and Y are two variables with X=5, Y=10 OPERATORS DESCRIPTION EXAMPLE + Adds both operands X + Y = 15 Subtracts both operands Y-X=5 * Multiply both operands X * Y = 150 / Divide both operands Y/X=2 % Y/X=0 Remainder after division of numbers ++ Increase operator by 1 X++ = 6 -Decrease operator by 1 Y-- = 9 2) Relational Operators Relational Operators perform comparison between the variables. It gives Boolean values, if the condition is fulfilled it becomes true otherwise false. Example Suppose A=5 and B=10. OPERATORS > < >= <= != == DESCRIPTION EXAMPLE Checks which operand is (A>B) False greater than the other. (B>A) True Checks which operand is (A<B) True smaller. (A<B) False Checks whether the operand is (A>=B) False greater than or equal to the other (B>=A) True operand. Checks whether the operand is (A<=B) True smaller than or equal to the other (B>=A) False operand. Checks whether both the operands are not equal. Checks whether both the operands values is equal. (A!=B) True (A==B) False 3) Logical Operators Logical operators check whether the condition is true or false. Example: Suppose if X=10 and Y=5 OPERATORS DESCRIPTION EXAMPLE AND (X==10 && Y==5) TRUE TRUE: If both the conditions are true. || True: If only one condition is true. (X==10 && Y==8) TRUE ! It reverses the state of its operand. !(X==Y) TRUE 4) Assignment Operators The assignment operator is used to assign a value to a variable. x=5. It means, 5 is assigned to variable to x. x=y; It means, the value of y is assigned to x. 5) Compound Operators They modify the variable by performing an operation on it. Example: #include <iostream> using namespace std; void main(){ int a=0,b=1,c=2,d=4,e=8; a+=2; cout<<"Value of a is "<<a; b-=8; cout<<"Value of b is "<<b; c*=4; cout<<"Value of c is "<<c; d/=2; cout<<"Value of d is "<<d; e%=4; cout<<"Value of e is "<<e; } Output: Value of a is 2 Value of b is -7 Value of c is 8 Value of d is 2 Value of e is 0 NAMED CONSTANT A named constant is like a variable, but its content is read-only, and cannot be changed while the program is running. Here is a definition of a named constant: const double INTEREST_RATE = 0.129; It looks just like a regular variable definition except that the word const appears before the data type name, and the name of the variable is written in all uppercase characters. The key word const is a qualifier that tells the compiler to make the variable read-only. Its value will remain constant throughout the program’s execution. It is not required that the variable name be written in all uppercase characters, but many programmers prefer to write them this way so they are easily distinguishable from regular variable names. Example: // This program calculates the area of a circle. // The formula for the area of a circle is PI times // The radius squared. PI is 3.14159. #include <iostream> #include <math.h> // needed for pow function (See Book Page 127 for more information) using namespace std; int main() { const double PI = 3.14159; double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = PI * pow(radius, 2.0); cout << "The area is " << area << endl; return 0; } Output: This program calculates the area of a circle. What is the radius of the circle? 2 The area is 12.56