Professional Documents
Culture Documents
Course Instructor:
Lab Instructor:
Below is the table having size and ranges of datatypes when combined with the type modifiers:
Type Size in bytes Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int 2bytes 0 to 65,535
signed short int 2bytes -32768 to 32767
long int 8bytes -2,147,483,648 to 2,147,483,647
signed long int 8bytes same as long int
unsigned long int 8bytes 0 to 4,294,967,295
long long int 8bytes -(2^63) to (2^63)-1
unsigned long long 8bytes 0 to 18,446,744,073,709,551,615
int
float 4bytes
double 8bytes
long double 12bytes
wchar_t 2 or 4 bytes 1 wide character
Escape Sequences:
Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals
and printers. They are also used to provide literal representations of nonprinting characters and characters that
usually have special meanings, such as the double quotation mark (").
Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called
"escape sequences." Escape sequence can be used to represent a newline character, a horizontal tab or any other
special character. The following table lists some of the ANSI escape sequences and what they represent.
\b Backspace
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
Mathematical operators perform mathematical operation such as +, -, *, % and /. C/C++ also has unary
mathematical operators (++ and --) and binary mathematical operators.
Unary Operator:
Unary operators are named unary because they take a single operand as shown in table
These operators can be used only with variables not with constants. These are used to add 1 or to subtract 1
from the operand.
++x same as x = x + 1
--y same as y = y + 1
++x and --y are called prefix mode, that means the increment or decrement operators modify their
operand before it is used.
x++ and y-- are called postfix mode, the increment or decrement operators modify their operand
after it is used.
Operator Precedence:
C applies the operators in arithmetic expressions in a precise sequence determined by the following rules
of operator precedence, which are generally the same as those in algebra:
1. Operators in expressions contained within pairs of parentheses are evaluated first. Thus, parentheses
may be used to force the order of evaluation to occur in any sequence you desire. Parentheses are
said to be at the "highest level of precedence." In cases of nested, or embedded, parentheses, such
as ( ( a + b ) + c ) the operators in the innermost pair of parentheses are applied first.
2. Multiplication, division and remainder operations are applied first. If an expression contains several
multiplication, division and remainder operations, evaluation proceeds from left to right.
Multiplication, division and remainder are said to be on the same level of precedence.
3. Addition and subtraction operations are evaluated next. If an expression contains several addition
and subtraction operations, evaluation proceeds from left to right. Addition and subtraction also
have the same level of precedence, which is lower than the precedence of the multiplication,
division and remainder operations.
If the operators are in the same level, then, the operators are performed from left to right order, referring to
table. For example:
Lab Tasks
Task 1:
Write a program which takes 2 integers as input from user and prints their sum, product, difference,
product, division and remainder.
Sample Output
Task 2:
1. Write appropriate C++ statements to match the description in each of the following comments.
Sample Output
Now write the above program again but use float type for radius, diameter, circumference and area. Use
the value 3.14159 for π. What is the new output?
Task 3:
Follow the instructions given below to write a simple program and compile.
Declare the variable named “Base”, “Height” and “Area” initialized by any value. Your program should calculate
the Area of the Triangle. Hint:
Formula:
Sample Output
Base is 2
Height is 4
The area for the triangle with the base of 2 and the height 4 is 4.0.
Task 4:
Write a program that get your gpa of 5 courses and calculate your CGPA using following formula.
Task 5:
Where D= x + vt + 1/2at2
Task 6:
If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits.
(Hint: Use the modulus operator ‘%’).
Task 7:
1. b 2−4 ac
2. y = mx + c
Task 8:
Write a C++ Program for the Exercises 1-20, translate the phrase into a mathematical expression
involving the given variable.
Task 8:
seconds in minutes
minutes in hours
seconds in hours
days to years
months to years
years to century
centimeters to inches
centimeters to feet
inches to feet
inches to yards
centimeters to meters
feet to yards