Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Lecture No. 05 [Operators, Expressions and its Conversion into C & C++ Statements]

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture No. 05 [Operators, Expressions and its Conversion into C & C++ Statements]

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Discipline: BS (CS) 1st Semester

Subject: Programming Fundamentals


Week No.: 05 [Operators, Expressions and its Conversion into C/C++ Statements]
Prepared by: ARSHAD IQBAL, Lecturer (Computer Science),
Computer Science Department, CECOS University of IT & Emerging Sciences,
Peshawar

Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Topics:
 Operators
 Operators Categories
 Unary Operators
 Binary Operators
 Arithmetic Operators
 Addition (+)
 Subtraction (-)
 Multiplication (*)
 Division (/)
 Remainder (%)
 Relational Operators
 = = Equal to
 != Not equal to
 > Greater than
 < Less than
 >= Greater than or Equal to
 <= Less than or Equal to
 Logical
 && AND operator
 || OR operator
 ! Not operator
 Expressions
 Operator Precedence
 Conversion of Mathematical Expressions/Formulas into C/C++ statements

Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Operators:
 Operators in C/C++ are mostly made with signs.
 Operators are simply a symbol or collection of symbols to perform an action on operands.
Where operand is a variable or constant on which an operator will perform an action.
 E.g. a = b + c; Where a, b, c are operands and operator is +.
 Operators are used to make C/C++ code shorter, since it relies less in English words.

 The following are some C/C++ Operators:


 Arithmetic Operators
 Relational Operators
 Logical Operators

 The Operators can be categorized as follows:


 Unary Operators
 Binary Operators

Unary Operators:
 A type of operator that works with one operand is known as unary operator.
 Following operators are unary operators:
 -, ++, --
 The above operators are used with one operand as follows:
 -a;
 N++;
 --x;

Binary Operators:
 A type of operator that works with two operands is known as binary operator.
 Following operators are binary operators:
 -, +, *, /, %.
 The above operators are used with two operands as follows:
 a + b;
 x/y;

Arithmetic Operators:
 The arithmetic operators are the symbols that performs mathematical operations on data.
 These are used in arithmetic expressions.
 Each arithmetic operator operates upon two numeric values (constants or variables) and returns
a value.
 The following arithmetic operators are used in C/C++:

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (for remainder)
 All arithmetic operators, except the remainder operator, are used for all type of numeric data.

Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
 Some important points about modulus operator are as follows:
 Modulus operator is also called remainder operator.
 The remainder or modulus operator (% operator) can only be used for integer type data. It returns
the remainder when one integer is divided by another integer.
 For example, 5 % 3 returns the remainder 2.
 If Modulus operator is used with the division of 0, the result will always be 0. For example,
the expression 0 % 5 will give 0 as a result.
 Inexpression like 3 % 5, 3 is not divisible by 5. Its result is 3.

Program 01: Write a program which uses arithmetic operators (+, -, *, /, %).

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, sum, subt, mult, div, rem;
cout<< “please enter the value of num1”;
cin>>num1;
cout<< “please enter the value of num2”;
cin>>num2;
sum = num1 + num2;
subt = num1 - num2;
mult = num1 * num2;
div = num1 / num2;
rem = num1 % num2;
cout<< “sum = ” <<sum<<endl;
cout<< “subtraction = ” <<subt<<endl;
cout<< “multiplication = ” <<mult<<endl;
cout<< “division = ” <<div<<endl;
cout<< “remainder =” <<rem;
getch ();
}

Program 02: Write a program to perform addition, subtraction, multiplication and division of
two numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, sum, subt, mult, div;
cout<<” please enter the value of num1 “;
cin>>num1;
cout<<” please enter the value of num2 “;
cin>>num2;
sum=num1+num2;
subt= num1-num2;
mult=num1*num2;
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
div=num1/num2;
cout<<” sum = “ <<sum<<endl;
cout<<” subtraction = “<<subt<<endl;
cout<<” multiplication = “<<mult<<endl;
cout<<” division = “<<div;
getch();
}
Relational Operators:
 The operators that are used to specify a relation between two expressions or values are known
as relational operators.
 The relational operators are used to specify conditions in programs.
 A relational operator compares two values.
 It produces result as true or false.
 A relational expression is a statement that uses relational operators to compare two values. The result
of a relational expression can be true or false.
 The relational operators are sometimes called the conditional operators or comparison operators as
they test conditions that are either true or false.
 The following are relational operators in C/C++:
 == Equal to
 != Not equal to
 > Greater than
 < Less than
 >= Greater than or Equal to
 <= Less than or Equal to

Relational Expression:
 A relational expression is a statement that uses relational operators to compare two values.
 The result of a relational expression can be true or false.
 Both sides of a relational expression can be a constant, variable or arithmetic expression.
 Examples:
 100 > 15 True
 25 < 5 False
 30 <= 12 False
 40 >= 20 True
 0 >= 0 True
 0 <= 0 True
 1 != 5 True
 5 != 5 False

Program No. 03: Write a program that inputs marks and displays “Congratulations! You have
passed.” If the marks are 40 or more.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int marks;
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
cout<< “Enter your marks: ”;
cin>>marks;
if (marks>=40)
cout<< “Congratulation! You have passed.”;
getch ();
}

Program No. 04: Write a program that inputs two numbers and finds whether both are equal.

#include<iostream.h>
#include<conio.h>
void main ()
{
Clrscr ();
int a, b;
cout<< “Enter a number: ”;
cin>>a;
cout<< “Enter a number: ”;
cin>>b;

if (a==b)
cout<< “Both numbers are equal.”;
getch ();
}

Program No. 05: Write a program that inputs marks of three subject. If the average of marks is more
than 80, it displays two messages “You are above standard” and “Admission
granted!”.

#include<iostream.h>
#include<conio.h>
void main ()
{
Clrscr ();
int sub1, sub2, sub3;
float avg;
cout<< “Enter marks of first subject: ”;
cin>>sub1;
cout<< “Enter marks of second subject: ”;
cin>>sub2;
cout<< “Enter marks of third subject: ”;
cin>>sub3;
avg = (sub1+sub2+sub3)/3.0;
if (avg>80)
{
cout<< “You are above standard! \n”;
cout<< “Admission granted!”;
}
getch ();
}
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Logical Operators:
 The logical operators are used to combine relational expressions or relational conditions.
 Logical Operators are used to evaluate compound conditions.
 It is also called compound condition or compound expression.
 The expression containing logical operators is called logical expression or logical condition.
 The output of a logical expression is also in logical form.
 Its value is either true or false.

 In C/C++, the following Logical Operators are used:


 && AND Operator
 || OR Operator
 ! NOT Operator

Program No. 06: Write a program that inputs three numbers and displays the maximum number by
using logical operators.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c;
cout<< “Enter three numbers:”;
cin>>a>>b>>c;
if (a>b && a>c)
cout<< “Maximum number is ”<<a;
else if (b>a && b>c)
cout<< “Maximum number is ”<<b;
else
cout<< “Maximum number is ”<<c;
getch();
}

Program No. 07: Write a program that inputs a character and displays whether is a vowel or not.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<< “Enter any character: ”;
cin>>ch;
if (ch== ‘A’ || ch == ‘a’ || ch == ‘E’ || ch == ‘e’ || ch == ‘I’ || ch == ‘i’ || ch
== ‘O’ || ch == ‘o’ || ch == ‘U’ || ch == ‘u’;
cout<< “ You entered a vowel: ”<<ch;
else
cout<< “ You did not enter a vowel: ”<<ch;
getch();
}
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Expressions:
 A statement that evaluates a value is called an expression.
 An expression returns a single value after evaluating.
 An expression is a combination of variables, constants and operators.
 Every expression consists of at least one operand and can have one or more operators. Operands
are values/variables, whereas operators are symbols that represent particular actions.
 In the expression: X + 5 where X and 5 are operands, and + is an operator.
 The value of the expression is assigned to a variable on the left side of ‘=’ operator. This variable is
known as the receiving variable. The operator ‘=’ is called assignment operator. It is used to assign
the calculated value of the expression to the receiving variable. The receiving variable must be of
numeric type.

 For example: If m = 10, x = 5 the expression may be written as: m * x + 100


 In the above example, “m * x + 100” is an arithmetic expression.
 This expression returns a single value. The value is assigned to a single variable using the assignment
operator.
 Thus, if “res” is the receiving variable, the assignment statement is written as: res = m * x + 100.

 The Order of Precedence of Operation or Priority of Operators:


 The order in which different types of operators in an expression are evaluated is known
as order of precedence or priority of operators. It is also known as hierarchy of operations.
 Each operator has its own precedence level. If an expression contains different types of operators,
the operators with higher precedence are evaluated before the operators with lower precedence.
 When an arithmetic expression is evaluated, the computer performs only one operation at a time.

 The order of precedence in C/C++ Language is as follows:


 If parentheses are used in an expression, then expressions within parentheses are first
computed from left to right.
 Then all multiplications and divisions are performed first from left to right.
 Then all additions and subtractions are then performed (first additions and then subtractions)
from left to right.
 When parentheses are used within parentheses, then expression within innermost parentheses
is evaluated first.

 For example: (4 – (3*5)) + 2 is evaluated as follows:

 (3*5) is computed and returns value of 15

 4 – 15 is computed and returns value of -11

 -11 +2 is computed and returns value of -9

 Conversion of Mathematical Expressions/Formulas into C/C++ statements:


 Rules to follow while converting mathematical expressions to C/C++ statements:
 Unlike algebraic expressions, C/C++ do not assume/insert any operator (inside expressions) for
itself. Hence, you must insert the arithmetic operator wherever it’s assumed or necessary in
arithmetic, so:
(x + y) (l + m) should be (x + y) * (l + m)
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
 You should enclose sections with brackets to clarify the expression, more precisely:

x + y + c / d + e should be (x + y + c) / (d + e)

 C/C++ language does not have any exponentiation operator, so it is necessary to convert the raise
to the power with multiple (*) signs. For instance, in C/C++ languages, x 3 should be written by
x*x*x.

 Example:
5x2 + 2x + 3 should be 5 * x * x + 2 * x + 3

 Unlike arithmetic, for the multiplication operator, put proper multiple sigh (*) instead of small x:

m x b + h x i should be m * b + h * I

 For example:
Expression1: 3x
Expression2: 3x+y
Expression3: x +y /7
Expression4: 3x+y/z+2

Program08: Write a program to convert the above expressions into C/C++ statements.

# include <iostream.h>
# include <conio.h>

void main ()
{
clrscr ();
double x, y, z; /* You can assign x, y, z with a value you want or you can take user
input as below. */
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the value of y: ";
cin >> y;
cout << "Enter the value of z: ";
cin >> z;
cout << "Expression 1 is: " << 3 * x << endl;
cout << "Expression 2 is: " << (3 * x) + y << endl;
cout << "Expression 3 is: " << (x + y) / 7 << endl;
if (z != 0)
cout << "Expression 4 is: " << (3 * x) + (y / z) + 2 << endl;
else
cout <<"Divide by zero is not allowed, the value of z cannot be zero";
getch ();
}

Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.
Program 09: Write a program which implements the following formula: c = a 2 + 2ab + b2.

#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
int a, b, c;
cout<<” Please enter the value of a “;
cin>>a;
cout<<” Please enter the value of b “;
cin>>b;
c = (a*a) +(2*a*b) + (b*b);
cout<< “c = ”<<c;
getch ();
}
Program 10: Write a program which takes the value of temperature in centigrade as input and then
convert and display the temperature to Fahrenheit.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int c, f;
cout<<” Please enter a temperature in centigrade “;
cin>>c;
f = c * 5/9 + 32;
cout<<” Temperature in Fahrenheit = “<<f;
getch ();
}

Program 11: Write a program which takes a value from user and then display the square and cube of that
value.

#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
int num, square, cube;
cout<<” Please enter a number “;
cin>>num;
square=num*num;
cube=num*num*num;
cout<<” Square of “<<num<<” = “<<square<<endl;
cout<<” Cube of “<<num<<” = “<<cube;
getch ();
}
Prepared by: ARSHAD IQBAL, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging Sciences, Peshawar.

You might also like