Question

In: Computer Science

Describe numerical, relational, and logical operators and give a few examples for each.

Describe numerical, relational, and logical operators and give a few examples for each.

Solutions

Expert Solution

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.


Related Solutions

USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub...
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest): 5 *, /, % 4 +, - 3 <, <=, >, >=, !=, == 2 not 1 and 0 or 1.  Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the...
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at...
Relational Operators For Cpp Discuss why relational operators can be used with enumeration types. Provide at least 2 examples of relational expressions with enumeration types. Explain your examples
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
Define a compensable factor and give a few examples
Define a compensable factor and give a few examples
Use logical operators in descriptions of rules and relationships in a problem situation.
Use logical operators in descriptions of rules and relationships in a problem situation.
Describe the major prospecting methods and give examples of each method
Describe the major prospecting methods and give examples of each method
Explain how Gains and on disposal of assets losses are calculated and give numerical examples for...
Explain how Gains and on disposal of assets losses are calculated and give numerical examples for taxable gains and non-taxable gains. (DO NOT copy from the Law). . TAX AND ZAKAT IN SAUDI ARABIA THE LAWS AS FOLLOW: The gain or loss from the disposal of an asset is the difference between the compensation received and its cost base No gain or loss on disposal of a depreciable asset is taken into account other than what is stated in Article...
Explain how Gains and on disposal of assets losses are calculated and give numerical examples for...
Explain how Gains and on disposal of assets losses are calculated and give numerical examples for taxable gains and non-taxable gains ?
a) Explain and give a numerical example of time dilation. b) Explain and give a numerical...
a) Explain and give a numerical example of time dilation. b) Explain and give a numerical example of length contraction.
List, describe and give examples of the value of each of the following types of Variance...
List, describe and give examples of the value of each of the following types of Variance Analysis, "volume variance," "spending/quantity use variance," "static budget variance" and "flexible budget variance." How have you seen these used? How would you apply any of these to your personal life?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT