Introduction to Computing Lab 9: Understand Arithmetic and logic operations
LAB # 9
To Understand The Arithmetic And Logic Operations
Objective
Learn to write programs using different arithmetic and logical operators
Theory
Arithmetic Operators:
Arithmetic Operators are used to do basic arithmetic operations like addition, subtraction,
multiplication, division, and modulus.
Types of Arithmetic Operators:
The following table lists the arithmetic operators used in C++.
Operator Action Description
+ Addition Adds two operands
- Subtraction Subtracts second operand from the first
* Multiplication Multiplies both operands
/ Division Divides numerator by de-numerator
Modulus Operator and remainder of after an
% Modulus
integer division
Sample Program
#include <iostream>
Using namespace std ;
int main( )
{
int x = 13;
int y = 6;
int a = x%y;
int b = x+y;
cout << "The Modulus of x,y is ::" << a << '\n' ;
cout << "The Sum of x,y is ::" << b << '\n' ;
return 0;
}
Program Output:
The Modulus of x,y is :: 1
The Sum of x,y is :: 19
1
Introduction to Computing Lab 9: Understand Arithmetic and logic operations
Unary Arithmetic Operators
Unary operators are operators that only take one operand. There are two unary arithmetic
operators, plus (+), and minus (-). These are used as increment and decrement operators.
Increment ++ Increment operator, increases integer value by one
Decrement -- Decrement operator, decreases integer value by one
Examples
x = x+1; x = x-1;
is the same is the same
as x++; as x--;
Sample Program:
#include <iostream>
Using namespace std ;
main()
{
int a = 21;
int c ;
// Value of a will not be increased before assignment.
c = a++;
cout << "Value of a++ is :" << c << endl ;
// After expression value of a is increased
cout << "Value of a is :" << a << endl ;
// Value of a will be increased before assignment.
c = ++a;
cout << "Value of ++a is :" << c << endl ;
return 0;
}
Program Output:
Value of a++ is :21
Value of a is :22
Value of ++a is :23
2
Introduction to Computing Lab 9: Understand Arithmetic and logic operations
Assignment operator (=)
The assignment operator assigns a value to a variable. The assignment operation always takes
place from right to left.
e.g. x = 5;
This statement assigns the integer value 5 to the variable x.
Sample Program:
#include <iostream>
Using namespace std ;
int main ()
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
cout << "a:";
cout << a;
cout << " b:";
cout << b;
}
Program Output:
a:4 b:7
Relational Operators:
In the following table different relational operators are listed that are supported by C++
language
Operator Description Example
== Checks if the values of two operands are equal or not (A == B)
!= Checks if the values of two operands are equal or not (A!= B)
Checks if the value of left operand is greater than the
> (A > B)
value of the right operand
Checks if the value of left operand is less than the
< (A < B)
value of the right operand
Checks if the value of left operand is greater than or
>= (A >= B
equal to the value of the right operand
<= Checks if the value of left operand is less than or equal (A <= B)
to the value of the right operand
3
Introduction to Computing Lab 9: Understand Arithmetic and logic operations
Sample Program
#include <iostream> Program Output:
Using namespace std ;
main() a is not equal to b
{ a is not less than b
int a = 21; a is greater than b
int b = 10;
int c ;
if( a == b )
{
cout << “a is equal to b" << endl ;
}
else
{
cout << "a is not equal to b" << endl ;
}
if ( a < b )
{
cout << "a is less than b" << endl ;
}
else
{
cout << "a is not less than b" << endl ;
}
if ( a > b )
{
cout << "a is greater than b" << endl ;
}
else
{
cout << "a is not greater than b" << endl ;
}
return 0;
}
Logical Operators:
There are following logical operators supported by C++ that are defined in the table below:
Operator Description Example
Called Logical AND operator. If both the operands are
&& non-zero, then condition becomes true. (A && B)
Called Logical OR Operator. If any of the two
|| operands is non-zero, then condition becomes true. (A || B)
Called Logical NOT Operator. Use to reverse the
! logical state of its operand. If a condition is true, then !(A && B)
Logical NOT operator will make false.
4
Introduction to Computing Lab 9: Understand Arithmetic and logic operations
Sample Program
#include <iostream> Program Output:
Using namespace std ;
main() Line 1 - Condition is true
{ Line 2 - Condition is true
int a = 5; Line 4 - Condition is not true
int b = 20; Line 5 - Condition is true
int c ;
if ( a && b )
{
cout << "Line 1 - Condition is true"<< endl ;
}
if ( a || b )
{
cout << "Line 2 - Condition is true"<< endl ;
}
/* Let's change the values of a and b */
a = 0;
b = 10;
if ( a && b )
{
cout << "Line 3 - Condition is true"<< endl ;
}
else
{
cout << "Line 4 - Condition is not true"<< endl ;
}
if ( !(a && b) )
{
cout << "Line 5 - Condition is true"<< endl ;
}
return 0;
}
5
Introduction to Computing Lab 9: Understand Arithmetic and logic operations
Lab Task
Lab Task 9.1) Write a program that prints the area and the parameter of the rectangle
Area of rectangle: LxB
Parameter of rectangle: 2(L+B)
Sample Output
Enter length of the rectangle: 6
Enter breadth of rectangle: 9
Area of a rectangle is 54
Parameter of rectangle is 30
Lab Task 9.2) Write a program that checks whether the entered alphabet is a vowel or not
Lab Task 9.3) Write a program that checks whether the number entered as input is odd or
even.
Lab Task 9.4) Write a program that takes two numbers as input and then calculate and
print the sum, difference, product and quotients of these two numbers
Sample Output
Enter first number: 6
Enter second number: 2
Sum: 8
Difference: 4
Product: 12
Quotient: 3
Note: Attach with manual every above mentioned task.