Lab No4
Lab No4
4
Use of arithmetic, relational, logical, bitwise and assignment
operators
Objective :
C++ programming has various operators to perform tasks including arithmetic,
conditional and bitwise operations. You will learn about various C operators and how
to use them in this experiment.
Tools:
Dev C++
Background Knowledge:
An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. C language is rich in built-in operators and provides the following
types of operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
The following table4.1 shows all the arithmetic operators supported by the C++
language. Assume variable A holds 10 and variable B holds 20
Relational Operators
The following table 4.2 shows all the relational operators supported by C++. Assume
variable A holds 10 and variable B holds 20, then:
Logical Operators
Following table 4.3 shows all the logical operators supported by C+ language.
Assume variable A holds 1 and variable B holds 0, then:
Bitwise Operators
Bitwise operators work on bits and perform bit-by-bit operation. The truth table for
&, |, and ^ is as follows as shown in Table 4.4
Sample Tasks
Task 1
Write a program to convert millimeter in to inches and display the result on the screen
#include<iostream>
using namespace std;
int main ()
{
float mm,inch;
mm=100.26 ;
inch=mm/25.4;
cout<<mm<<<inch<<”inches”;
}
Task 2
Write a program to add two floating point values and display the sum on the screen
#include<iostram>
using namespace std;
int main ()
{
float a,b,s;
a=10.2;
b=5.5;
s=a+b;
cout<<”Sum of “<< a<<”and”<<b<<”is=”<<s;
}
Task 3
Write a program that convert 37 centigrade temperature in to Fahrenheit using the
formula F=9/5 (C +32) and display the temperature in Fahrenheit.
#include<iostream>
using namespace std;
int main ()
{
float f,c;
c=37;
f=9/5 *c + 32;
cout<<c<<”centigrade=”<<f<<”Fahrenheit”; }
Exercise :
Exercise 1:
Write a program to input any two numbers from user and to perform all arithmetic
operators available in C++
Solution:
Write your code here
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 2
Write a program to input any two numbers from user and to perform all relational
operators
Solution:
Write your code herecZ
Marks Obtained Total Marks
Instructor Signatures:
Remarks:
Exercise 3
Write a program to input any two numbers from user and to perform all logical
operators
Solution:
Write your code here
Instructor Signatures:
Remarks:
Exercise 4:
Write a program to swap two variables using arithmetic operation without using the
3rd variable
Solution:
Write your code here
Instructor Signatures:
Remarks:
FINAL EVALUTION LAB 04
Student Name:
Registration Number:
Date:
Instructor Signatures:
Remarks: