In: Computer Science
Describe numerical, relational, and logical operators and give a few examples for each.
1) -------------Numerical operator----------------
Numerical Operators are the type of operators which take numerical values (either literals or variables) as their operands and return a single numerical value.
| Operator | Description | Example |
|---|---|---|
| + | Adds operands | a+b=12 |
| - | Subtracts second operand from the first | a-b=4 |
| * | Multiplies both operands | a*b=32 |
| / | Divides numerator by denominator. | a/b=2 |
| % | Modulus Operator returns the remainder after an integer division. | a%b=0 |
Example:-
int main()
{
int a = 25;
int b = 8;
printf("sum = %d", (a+b));
printf("\ndifference = %d", (a-b));
printf("\nproduct = %d", (a*b));
printf("\nremainder = %d\n", (a%b));
return 0;
}
output:
sum = 33
difference = 17
product = 200
remainder = 1
Now let's talk about '/'.
If we divide two integers, the result will be an integer.
5/2=2 (Not 2.5)
To get 2.5, at least one of the numerator or denominator must have a decimal(float) value.
5.0/2=2.5 or 5/2.0=2.5 or 5.0/2.0=2.5 but 5/2 = 2.
#include <stdio.h>
int main()
{
int a ;
int b ;
a = 2 ;
b = 9 ;
printf ( "Value of b/a is : %d\n" , b/a ) ;
return 0;
}
Value of b/a is : 4
#include <stdio.h>
int main()
{
float a ;
int b ;
a = 2.0 ;
b = 9 ;
printf ( "Value of b/a is : %f\n" , b/a ) ;
return 0;
}
Value of b/a is : 4.500000
As we saw, if 'b' and 'a' are both integers, then the result is 4 (not 4.5) but when one of them is float then the result is 4.500000 (a float).
2) -----------------------------------Relational operator-----------------------------------
Relational Operators check the relationship between two operands. If the relationship is true, it returns 1, if the relationship is false, it returns 0.
| Operator | Description | Example |
|---|---|---|
| == | Equal to | (a == b) is false |
| != | Not equal to | (a != b) is true |
| > | Greater than | (a > b) is true |
| < | Less than | (a < b) is false |
| >= | Greater than or equal to | (a >= b) is true |
| <= | Less than or equal to |
(a <= b) is false |
Example
#include <stdio.h>
int main()
{
int a = 5, b = 6;
printf("\n%d", a == b);
printf("\n%d", a != b);
printf("\n%d", a > b);
printf("\n%d", a < b);
printf("\n%d", a >= b);
printf("\n%d", a <= b);
return 0;
}
output--
0
1
0
1
0
When the expression was true, we got 1 and when false, 0.
3)---------------------------------- Logical operators---------------------------------
The symbol for AND is && while that of OR is ||.
| Operator | Description | Example |
|---|---|---|
| && | Logical AND. If both the operands are non-zero, then the condition becomes true | (a == b) && (a==8) is false |
| || | Logical OR. If any one or both the operands are non-zero, then the condition becomes true | (a != b) || (a==5) is true |
| ! | Logical NOT. It is used to reverse the condition. So, if a condition is true, ! makes it false. |
!(a==5) is true |
Example:---
#include <stdio.h>
int main()
{
int a = 5, b = 0;
printf("\n%d", a && b);
printf("\n%d", a || b);
printf("\n%d", !a);
printf("\n%d", !b);
return 0;
}
output:--
0
1
0
1
Since a is non-zero but b is zero, so AND between them will be false (or 0). As only one of them is true (or non-zero). But with OR, since anyone of them (i.e. a) is non-zero, so, a||b is true (or 1 ).
In this example, since the value of 'a' is non-zero, therefore it is true. So, !a makes it false. The case with !b is the opposite.