Unit 3 Operators and Expressions 4 Hrs.
Unit 3 Operators and Expressions 4 Hrs.
Operators
An operator is a symbol that operates on a certain data item.
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
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.
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.
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
f. Conditional Operators
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n1, n2, larger;
clrscr();
Output
Enter two numbers: 25 22
The larger number is 25
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 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.
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();
}
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;
Output
Enter two numbers: 25 22
The larger number is 25
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