Professional Documents
Culture Documents
5.arithmetic Expression - Reviewed
5.arithmetic Expression - Reviewed
Definition
• Expression is the combination of the
constants, variables, and operators arranged
according to the syntax of C++ language.
• Examples:
int x =10; float y = 2.5;
cout<< x+y / 5;
• Expression consists of
– Operand such as variable, constant
– Operator such as + - * / > <
• Types of operators based on the number of
operands:
– unary: negative such as -7 , !x , sizeof(x), ++x --x
– Binary: minus 2-1 x*y a/2 100%3
– Ternary: condition? Value1 : value2 ;
Arithmetic Operators
• C++ provides the following arithmetic operations:
+ (addition)
binary - (subtraction)
unary - (negative sign)
* (multiplication)
/ (division)
% (remainder) mod
• The % operator applies only to integer values
Cout<< 5%2; // it prints 1
Cout<< 4%2; // 0
Cout<< 9%3 ; // 0
Cout<<9%2; // 1
Cout<< 10%3; // 1
Cout<<19% 4 ; // 3
Cout<< 6/3 ; // 2
Cout<< 9/2; // 4
Cout<< 9.0 / 4.0 ; // 4.5
Cout<<39 /10 ; // 3.9 - it prints 3
Cout<<39 %10 ; // 9
Cout<<2154678 / 10 ; // 215467
Cout<<2154678%10 ; // 8
Cout<<123/100; // 1.23 it prints 1
Cout<<123%100; // it prints 23
Cout<< 300 % 1000 ; //300
Cout<< 300 / 1000 ; // 0
Cout<<(x+y+z) / 3;
Cout << 2.5 % 3 ; // error
% is used only for integers
Priorities
• Priorities between arithmetic operations:
1. parenthesized expressions
2. unary operators negative ! ++ --
3. / * %
4. + binary –
5. Assign operator =
• Operators with an equal priority are
performed left to right.
Example
1+2*-4-8+6/2%(2+4)
1+(2*(-4))-8+6/2%6
1 + (-8) – 8 +(6/2)%6
1+(-8) -8 + 3 % 6
1 + (-8) -8 + 3
-7 – 8 + 3
-15 + 3
-12
Arithmetic Expressions
• Expressions can contain arithmetic operators,
numeric values, variables, character values
#include<iostream>
using namespace std;
int main()
{ int x,y,z;
cout<<“Input 3 integers:”;
cin>>x>>y>>z;
cout<<“average=”<<(x+y+z)/3.0;
cout<<endl;
}
Example: Write a program to read compute the area of a rectangle. For
example,
Input rectangle length:1.2
input rectangle height: 10.2
area= 14.4
#include<iostream>
using namespace std;
int main(){
double L,H;
cout<<“Input rectangle length:”;
cin>>L;
cout<<“Input rectangle height:”;
cin>>H;
cout<<“area=”<<L*H;
cout<<endl;
}
Example: Write a program to convert from fahrenhiet to celsius. For
example,
Input fahrenhiet temperature:32
The celsius equivalent is: 0
#include<iostream>
using namespace std;
int main(){
double f;
cout<<“Input fahrenhiet temperature:”;
cin>>f;
cout<<“The celsius equivalent is:”<<(5/9)*(32-f);
cout<<endl;
}
Type casting
• Type casting refers to the conversion of one data
type to another in a program.
Operator
(+ - * /) char int float double
#include <iostream>
using namespace std;
int main()
{
float f = 3.5;
int a = f; // this is how you do in C
int b = static_cast<int>(f);
cout << b;
}
Operators in C++
• Operators in C++ can be classified into 6
types:
1. Arithmetic Operators. + * / - %
2. Assignment Operators. = += -= /= %= *=
3. Relational Operators. < <= > >= == !=
4. Logical Operators. && || !
5. Bitwise Operators. & | ^ >> << ~
6. Other Operators.
Arithmetic operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
Modulo Operation
% (Remainder after
division)
Bitwise Operators
In C++, bitwise operators perform operations on integer data at the individual
bit-level. These operations include testing, setting, or shifting the actual bits.
Operator Description
& Binary AND
| Binary OR
^ Binary XOR
Binary One's
~
Complement
<< Binary Shift Left
>> Binary Shift Right
Note: Bitwise operators can only be used alongside char and int data types.
C++ Bitwise AND Operator
The bitwise AND & operator returns 1 if and only if both the operands are 1. Otherwise, it
returns 0.
The following table demonstrates the working of the bitwise AND operator.
Let a and b be two operands that can only take binary values i.e. 1 and 0.
a b a&b
0 0 0
0 1 0
1 0 0
1 1 1
Example
#include <iostream>
using namespace std;
int main() {
// declare variables
int a = 12, b = 25;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
return 0;
}
00001100
& 00011001
----------------------
00001000 = 8 (In decimal)
Relational Operators
Operator Meaning Example
3 == 5 gives
== Is Equal To
us false
3 != 5 gives us
!= Not Equal To
true
3 > 5 gives us
> Greater Than
false
3 < 5 gives us
< Less Than
true
a b a&&b a||b !a
0 0 0 0 1
0 nonzero 0 1 1
nonzero 0 0 1 0
nonzero nonzero 1 1 0
Logical Operators Cont.
• && has higher precedence than ||
• && || have less precedence than any
relational operator
• ! has higher precedence than any
arithmetic operator
• Multiple || are performed from left to right
• Multiple && are performed from left to right
Example
cout<<8||2; // prints 1
cout<<-8&&2000; // prints 1
cout<<70||0; // prints 1
cout<<-5&&0; // prints 0
cout<<!100; // prints 0
cout<<!100&&100; // prints 0
cout<<0&&!0; // prints 0
int x=100;
cout<<(x>=0 && x<=200); // prints 1
cout<<!(x>=500); // prints 1
cout<<(!x>=500); // prints 0
cout<<!x||x&&x-x; // prints 0
Example
4>3*6+7%2-1>=15==3&&1!=7||5&&9||1+2 =
4>18+7%2-1>=15==3&&1!=7||5&&9||1+2 =
4>18+1-1>=15==3&&1!=7||5&&9||1+2 =
4>19-1>=15==3&&1!=7||5&&9||1+2 =
4>18>=15==3&&1!=7||5&&9||1+2 =
4>18>=15==3&&1!=7||5&&9||3 =
0>=15==3&&1!=7||5&&9||3 =
0==3&&1!=7||5&&9||3 =
0&&1!=7||5&&9||3 =
0&&1||5&&9||3 =
0||5&&9||3 =
0||1||3 =
1||3 =
1
Other operators
Operator Description Example
returns the
sizeof size of data sizeof(int); // 4
type
string result =
returns value
(5 > 0) ?
?: based on the
"even" :
condition
"odd"; // "even"
represents
# //
memory
& address of
address of the
num
operand
accesses
members of
. struct s1.marks = 92;
variables or
class objects
used with
pointers to
ptr->marks =
-> access the
92;
class or struct
variables
prints the
<< cout << 5;
output value
Positive numbers
cout << ceil(2.999);// 3
cout << ceil(2.333); // 3
Negative numbers
cout << ceil(-2.999);// -2
cout << ceil(-2.333); // -2
round
Positive numbers
cout << round(2.999);// 3
cout << round(2.0001); // 2
Negative numbers
cout << round(-2.999);// -3
cout << endl;
cout << round(-2.333); // -2
Examples
The math expression
a2 - |b|
is written in C as
pow(a,2)-fabs(b)
or as
a*a-fabs(b)
Examples
The math expression
a+sin(b2)+cos2(c)
ab - 1
log10(ab)
is written in C as
(a+sin(b*b)+pow(cos(c),2))/(a*b-1/log10(a*b))
Example: write a program to read 2 real values of x and y
and print the value of the expression:
x2 – y3
cos(x+y)
#include<iostream>
using namespace std;
int main()
{ int x,y;
cout<<“Input values of x and y:”;
cin>>x>>y;
cout<<“The value of the expression=“;
cout<<(x*x-pow(y,3))/cos(x+y);
}
Rand() function
rand() function is an inbuilt function in C++ STL, which is defined in header
file <cstdlib>. rand() is used to generate a series of random numbers.
int main()
{
// This program will create same sequence of
// random numbers on every program run
for (int i = 0; i < 5; i++)
cout << rand() << " ";
return 0;
}