Subject Name: PROGRAMMING FOR PROBLEM SOLVING
Subject Code:3110003
1-Arithmetic Operators:
Subject Code:3110003
Question: List all operators used in C and explain any three operators with example.List all operators:
- Arithmetic Operators.
- Relational Operators.
- Logical Operators.
- Assignment Operators.
- Increment and Decrement Operators.
- Conditional Operator.
- Bitwise Operators.
- Special Operators.
1-Arithmetic Operators:
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Operator | Meaning of Operator |
---|---|
+ | addition or unary plus |
- | subtraction or unary minus |
* | multiplication |
/ | division |
% | remainder after division (modulo division) |
Example 1: Arithmetic Operators
Output:
- // Working of arithmetic operators
- #include <stdio.h>
- int main()
- {
- int a = 9,b = 4, c;
- c = a+b;
- printf("a+b = %d \n",c);
- c = a-b;
- printf("a-b = %d \n",c);
- c = a*b;
- printf("a*b = %d \n",c);
- c = a/b;
- printf("a/b = %d \n",c);
- c = a%b;
- printf("Remainder when a divided by b = %d \n",c);
- return 0;
- }
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
- The operators +, - and * computes addition, subtraction, and multiplication respectively as you might have expected.
- In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
- It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
- The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1. The % operator can only be used with integers.
- Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,
// Either one of the operands is a floating-point number
a/b = 2.5
a/d = 2.5
c/b = 2.5
// Both operands are integers
c/d = 2
2-Relational Operators:
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator | Meaning of Operator | Example |
---|---|---|
== | Equal to | 5 == 3 is evaluated to 0 |
> | Greater than | 5 > 3 is evaluated to 1 |
< | Less than | 5 < 3 is evaluated to 0 |
!= | Not equal to | 5 != 3 is evaluated to 1 |
>= | Greater than or equal to | 5 >= 3 is evaluated to 1 |
<= | Less than or equal to | 5 <= 3 is evaluated to 0 |
Example 2: Relational Operators
Output:
- // Working of relational operators
- #include <stdio.h>
- int main()
- {
- int a = 5, b = 5, c = 10;
- printf("%d == %d is %d \n", a, b, a == b);
- printf("%d == %d is %d \n", a, c, a == c);
- printf("%d > %d is %d \n", a, b, a > b);
- printf("%d > %d is %d \n", a, c, a > c);
- printf("%d < %d is %d \n", a, b, a < b);
- printf("%d < %d is %d \n", a, c, a < c);
- printf("%d != %d is %d \n", a, b, a != b);
- printf("%d != %d is %d \n", a, c, a != c);
- printf("%d >= %d is %d \n", a, b, a >= b);
- printf("%d >= %d is %d \n", a, c, a >= c);
- printf("%d <= %d is %d \n", a, b, a <= b);
- printf("%d <= %d is %d \n", a, c, a <= c);
- return 0;
- }
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
3-Logical Operators:
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.
Operator | Meaning | Example |
---|---|---|
&& | Logical AND. True only if all operands are true | If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0. |
|| | Logical OR. True only if either one operand is true | If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1. |
! | Logical NOT. True only if the operand is 0 | If c = 5 then, expression !(c==5) equals to 0. |
Example 3: Logical Operators
Output:
- // Working of logical operators
- #include <stdio.h>
- int main()
- {
- int a = 5, b = 5, c = 10, result;
- result = (a == b) && (c > b);
- printf("(a == b) && (c > b) is %d \n", result);
- result = (a == b) && (c < b);
- printf("(a == b) && (c < b) is %d \n", result);
- result = (a == b) || (c < b);
- printf("(a == b) || (c < b) is %d \n", result);
- result = (a != b) || (c < b);
- printf("(a != b) || (c < b) is %d \n", result);
- result = !(a != b);
- printf("!(a == b) is %d \n", result);
- result = !(a == b);
- printf("!(a == b) is %d \n", result);
- return 0;
- }
(a == b) && (c > b) is 1Explanation of logical operator program:
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
- (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
- (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
- (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
- (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
- !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
- !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).