0% found this document useful (0 votes)
3 views

Unit 3 Operators and Expressions 4 Hrs.

The document explains operators and expressions in programming, defining operators as symbols that perform operations on operands. It categorizes operators into unary, binary, and ternary types based on the number of operands, and further classifies them into arithmetic, relational, logical, assignment, increment/decrement, and conditional operators. Additionally, it covers operator precedence and associativity, along with example programs demonstrating these concepts in C.

Uploaded by

crazyvenom30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 3 Operators and Expressions 4 Hrs.

The document explains operators and expressions in programming, defining operators as symbols that perform operations on operands. It categorizes operators into unary, binary, and ternary types based on the number of operands, and further classifies them into arithmetic, relational, logical, assignment, increment/decrement, and conditional operators. Additionally, it covers operator precedence and associativity, along with example programs demonstrating these concepts in C.

Uploaded by

crazyvenom30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Uint-5 Operators and Expressions

Operators
An operator is a symbol that operates on a certain data item.

Operators are used in programs to perform certain mathematical or logical manipulations.

For e.g. simple expression (8+3) the symbol „+‟ is called operator.

Operand:
The data items that operators act upon are called operands. In simple expression i.e. 8+3 „8‟
and „3‟ are operands.

Operation:
The acting of operator on operand is called operation.
In simple expression i.e 8+3 the symbol + does an addition operation.

Expressions

An expression is a combination of variables, constant and operators.

The following are the examples of expression: 8+3, a-b, a/b, a*b/3,
22a - 10b, a<b, etc.

Classify the types of operator on the basis of operand required for an operator.
Ans:
According to the number of operands required for an operator we can classify operators into:
1. Unary operators
The operators which require only one operand are known as unary operators. e.g.

i ++ (increment operators)
i - - (decrement operators)

2. Binary operators
The operator which requires two operands are known as binary operators.

e.g. a+b, a-b, a%b, a/b, a*b etc.

3. Ternary operators
 The operator which requires three operands are known as ternary operators.
 For e.g. the operator pair ? : (Conditional operator) is a ternary operator.

1 Prepared By: Er. Suraj Kumar Hekka


Explain the types of operator on the basis of action and utility.
According to the action and utility, operators are classified into various categories:

a. Arithmetic operators
e.g. +, -, *, /, %, etc.

b. Relational operators
e.g.
< is less than
> is greater than
<= is less than equal to
>= is greater than equal to
!= is not equal to
== is equal to

c. Logical operators:
There are three logical operators in C.
&& Logical AND
|| Logical OR
! Logical NOT
e.g:
A B A&&B A||B !A !B
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0

d. Assignment Operators

They are + =, - =, * =, / =, %=

Examples:
a+ =b Equivalent to a=a+b
a- =b Equivalent to a=a-b
a* =b Equivalent to a=a*b
a/ =b Equivalent to a=a/b
a% =b Equivalent to a=a%b

2 Prepared By: Er. Suraj Kumar Hekka


e. Increment and Decrement Operators

 The increment operator is used to increase the value of an operand by 1.

 The decrement operator is used to decrease the value of an operand by 1.

 They take only one operand, so also called unary operator.

f. Conditional Operators

 The operator pair ? : is known as conditional operator.

 It takes three operands. So, also called ternary operator.

 Its syntax is: expression1 ? expression2 : expression3

Program
#include<stdio.h>
#include<conio.h>
void main()

{
int n1, n2, larger;
clrscr();

printf(“Enter two number:”);


scanf(“%d %d”, &n1, &n2);

larger=n1>n2 ? n1: n2;

printf(“ The larger number is %d”, larger);


getch();

Output
Enter two numbers: 25 22
The larger number is 25

3 Prepared By: Er. Suraj Kumar Hekka


Operator Precedence and Associativity

 The precedence is used to determine how an expression involving more than one operator is
evaluated.

 There are distinct levels of precedence and an operator may belong to one of these levels.

 The operators at the higher level of precedence are evaluated first.

 The operators of the same precedence are evaluated either from left to right or right to left
depending on the level. This is known as associativity property of an operator.

 Table below shows the precedence level and its associativity.

Precedence level Operator Associativity


1 ( ), [ ] Left to right
2 ++, - - Right to left
3 *, /, % Left to right
4 +, - Left to right
6 <, <=, >, >= Left to right
7 == , ! = Left to right
11 && Left to right
12 || Left to right
13 ?: Right to left
14 *=, /=, %=, +=, - = Right to left
15 , Left to right

4 Prepared By: Er. Suraj Kumar Hekka


Lab no: 2
1. WAP to divide one integer by another integer and find the quotient and remainder.

Program
void main()
{
int a, b, quotient, remainder;
clrscr();
printf("Enter two integers:");
scanf("%d %d", &a, &b);
quotient =a/b;
remainder =a%b;
printf("The quotient is %d\n", quotient);
printf("The remainder is %d", remainder);
getch();
}

Output
Enter two integers: 10 3
The quotient is 3
The remainder is 1

2. WAP to read a four digit number and then find the sum of first and last digits.

Program
void main()
{
int num,a,b,sum;
clrscr();
printf("Enter any four digit number:\n");
scanf("%d",&num);

a=num%10;
b=num/1000;

sum=a+b;
printf("The sum of first and last digit numbers is %d",sum);
getch();
}

5 Prepared By: Er. Suraj Kumar Hekka


Output
Enter any four digit number: 4556
The sum of first and last digit numbers is 10

3. WAP in C to find the larger number among any two numbers using ternary operator.

Program
void main()
{
int n1, n2, larger;
printf(“Enter two number:”);
scanf(“%d %d”,&n1, &n2);

larger=n1>n2 ? n1 : n2;

printf(“The larger number is %d”, larger);


getch();
}

Output
Enter two numbers: 25 22
The larger number is 25

4. WAP in C to find largest among three numbers using ternary operator

Program
int main()
{
int n1 = 5, n2 = 10, n3 = 15, max;
clrscr();

max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);

printf("Largest number among %d, %d and %d is %d.", n1, n2, n3, max);
getch();
return 0;
}

Output
Largest number among 5, 10 and 15 is 15

6 Prepared By: Er. Suraj Kumar Hekka

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy