Lab Manual 04 - Arithmetic Operators, Expressions and Math Functions.
Lab Manual 04 - Arithmetic Operators, Expressions and Math Functions.
Lab- 04 Manual
Lab Instructor: Awais Amin
Department of Computer Science
Email: [email protected]
Lab 04: Arithmetic Operators, Arithmetic Expressions and Math
Functions
Table of Content
s
4.1 Objective................................................................................................................................................2
4.2 Scope......................................................................................................................................................2
4.3 Useful Concepts....................................................................................................................................2
4.4 Arithmetic Expressions:..........................................................................................................................2
4.5 Math function:.........................................................................................................................................3
4.7 Type Casting in C++...................................................................................................................8
4.8 Examples:............................................................................................................................................10
4.9 Exercise for Lab..................................................................................................................................13
4.10 Home Task...........................................................................................................................................14
Lab Rubrics
4.1 Objective
Learn using operators, expressions and math functions.
4.2 Scope
The student should know the following at the end of this lab:
1. Arithmetic Operators
2. Arithmetic Expressions
3. Math Functions
log10(x) log10 x
(x > 0) log10(5) = 0.698970
exp(x) ex exp(2) = 7.3891
sin(x) sin x (x in radian) sin(90) = 0.893997
4.6. 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:
To solve most programming problems, you will need to write arithmetic expressions that manipulate type
int and double data. In C language, these are the basic arithmetic operators: addition (+), subtraction (-),
multiplication (*), division (/), and remainder (%).
If the operands are of different types, the one with weaker type will be raised to the level of the other type,
then the operation will be performed. The order is char, int, long, float, double.
The division between two integer numbers results in an integer result (as you can see 5/2 gives 2 not 2.5). The
division by zero is undefined. Either you get a compilation warning or a run time error.
The remainder operator % is related to the division one. This operator will give the remainder produced by
dividing the two numbers (5 % 2 = 1). Therefore, this operator is not used with double values. If one of the
operand or both are double, you will get an error message from the compiler saying: illegal use of floating
point
Basic arithmetic operators in C++ are: *, /, -, +, ++, -- are the arithmetic operators.
Example:
Suppose X and Y are two variables with X=5, Y=10
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.
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 TRUE: If both the conditions (X==10 && Y==5) TRUE
are true.
|| True: If only one condition is (X==10 && Y==8) TRUE
true.
! It reverses the state of its !(X==Y) TRUE
operand.
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:
Output:
This program calculates the area of a circle. What is the radius of the
circle? 2
The area is 12.56
Declare a constant integer named `MAX_SCORE` and set it to 100. Then, declare a variable named
`playerScore` and initialize it with a score between 0 and `MAX_SCORE`. Display the player's score and the
maximum possible score.
4. String Manipulation:
Declare two string variables, `firstName` and `lastName`, and initialize them with your first and last name,
respectively. Concatenate these two strings to form your full name and display it.
Code:
#include <iostream>
using namespace std;
int main ()
{
short x = 200;
int y;
y = x;
cout << " Implicit Type Casting " << endl;
cout << " The value of x: " << x << endl;
cout << " The value of y: " << y << endl;
Code:
#include <iostream>
using namespace std;
int main ()
{
// declaration of the variables
int a, b;
float res;
a = 21;
b = 5;
cout << " Implicit Type Casting: " << endl;
cout << " Result: " << a / b << endl; // it loses some information
4.8Examples:
Example 4.1: This program illustrates some arithmetic properties of integer (int) variables.
Trace through the program to make sure you understand the values of each variable in all
stages of the program.
#include <iostream>
using namespace std; int
main()
{
int a=4, b=0, c=0;
cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl; b=6;
c=a+b;
cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl;
Example 4.2: Input a 3-digit value from the user (for example 521) and display it
in reverse order (i.e. 125).
#include <iostream>
using namespace std; int
main()
{
int n,a,b;
cout<<"Enter the 3 digit value to be reversed:"; cin>>n;
a=n/
100;
n=n
%100;
b=n/
10;
n=n
%10;
cout<<"The reverse order is:"<<n<<b<<a<<endl; return 0;
}
Example 4.3: Input the values for base and exponent and calculate its power using the
pow(x,y) built in function.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int expo;
double base, result=0;
cout<<"enter the value for the base:";
cin>>base;
cout<<"enter the value for the exponent:"; cin>>expo;
result = pow(base,expo);
cout<<"The result is: "<<result<<endl; return
0;
}
Exercise 4.1: Write a program that finds the area of a triangle given the length of its sides: a, b, c.
area s s a s b s c
abc
s 2
Exercise 4.2: Write a program taking two values as inputs from the user and display the results for
all the basic arithmetic operations performed on them
Addition
Subtraction
Multiplicatio
n Division
Modulus
Exercise 4.3: Write a program that inputs a 4 digit value from the user (for example 6382) and