4. Arithmetic Operators
Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x
5. #include<stdio.h>
int main()
{
int a, b, add, sub, mul, div, rem;
printf("Enter a, b values : ");
scanf("%d%d",&a,&b); // Reading two values
add=a+b; // Addition Operator
sub=a-b; // Subtraction Operator
mul=a*b; // Multiplication Operator
div=a/b; // Division Operator
rem=a%b; // Remainder (Modulo) Operator
printf("Result of addition is=%dn", add);
printf("Result of subtraction=%dn", sub);
printf("Result of multiplication is=%dn", mul);
printf("Result of division is=%dn", div);
printf("Result of remainder=%dn",rem);
return 0;
}
6. Assignment Operators
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Assignment operators are
used to assign values to
variables.
7. Comparison
Operators
• Comparison operators are used to
compare two values (or variables)
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
8. Logical
Operators
• Used to determine the logic between
variables or values
Operator Name Description Example
&& Logical and Returns true if both
statements are true
x < 5 && x < 10
|| Logical or Returns true if one of the
statements is true
x < 5 || x < 4
! Logical not Reverse the result,
returns false if the result
is true
!(x < 5 && x <
10)
11. Example showing use
Operator
of Relational
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
return 0;
}
12. Example showing use
Operator
of Logical
#include <stdio.h>
int main()
{
int m=40,n=20;
int a=20,p=30;
if (m>n && m !=0)
{ printf("&& Operator : Both conditions are truen"); }
if (a>p || p!=20)
{ printf("|| Operator : Only one condition is truen"); }
if (!(m>n && m !=0))
{
else
printf("! Operator : Both conditions are truen"); }
{ printf("! Operator : Both conditions are true. "
"But, status is inverted as falsen");
}
return 0;
}
13. BIT WISE OPERATORS IN C
These operators are used to perform bit operations.
Decimal values are converted into binary values
which are the sequence of bits and bit wise operators
work on these bits. Following are bitwise operator
1)
2)
3)
4)
5)
6)
&
|
~
^
<<
>>
Bitwise AND
Bitwise OR
Bitwise NOT
XOR
Left Shift
Right Shift
14. Program showing Bitwise Operator
in C
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %dn",AND_opr );
printf("OR_opr value = %dn",OR_opr );
printf("NOT_opr value = %dn",NOT_opr );
printf("XOR_opr value = %dn",XOR_opr );
printf("left_shift value = %dn", m << 1);
printf("right_shift value = %dn", m >> 1);
}
15. CONDITIONAL OR TERNARY
OPERATORS IN C
Conditional operators return one value if condition
is true and returns another value is condition is false.
This operator is also called as ternary operator.
Syntax
Example
:
:
(Condition? true_value: false_value);
(A > 100 ? 0 : 1);
16. Program for use of Ternary
Operator
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %dn", x);
printf("y value is %d", y);
return 0;
}
17. Increment / Decrement
Operators
Increment operators are used to increase the value of
the variable by one and decrement operators are
used to decrease the value of the
programs.
Increment operator
variable by one in C
++var_name; (or) var_name++;
Decrement operator
- -var_name; (or) var_name – -;
20. Exercises
Arithmetic Operators
i. Write a program to determine whether a number is divisible
by 5 or not
ii. Write a program to determine if a number is even or odd
iii. Write a C program to check whether a given number is
positive or negative
Relational Operators
i. Write a program to give a discount of 10% if amount of items
purchased exceeds Sh5000
21. Exercise - Logical operators
i. Write a program to check if a person is eligible to vote. The person
must be a Kenyan Citizen and above 18 years
ii. Write a program to implement the following requirements.
Requirement
A bank will offer a customer a loan if they are 21 or over and have an
annual income of at least Sh21,000. The customers age and income are
input in response to user friendly prompts. Write a program that will
output the following
Expected output
• Congratulations you qualify for a loan.
• Unfortunately, we are unable to offer you a loan at this time .