Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).
Operator |
Meaning |
+ |
addition or unary plus |
- |
subtraction or unary minus |
* |
multiplication |
/ |
division |
% |
remainder after division(modulo division) |
Write a C program demonstrating the use of all arithmetic operators:
#include <stdio.h>
int main() {
// Declare and initialize variables
int a = 10, b = 5;
// Addition
int sum = a + b;
printf("Addition: %d + %d = %d\n", a, b, sum);
// Subtraction
int difference = a - b;
printf("Subtraction: %d - %d = %d\n", a, b, difference);
// Multiplication
int product = a * b;
printf("Multiplication: %d * %d = %d\n", a, b, product);
// Division
int quotient = a / b;
printf("Division (integer): %d / %d = %d\n", a, b, quotient);
// Modulus (remainder of division operation)
int remainder = a % b;
printf("Modulus (remainder): %d %% %d = %d\n", a, b, remainder);
return 0;
}
A relational operator checks and establishes 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. The result of relational expression is always either 1(TRUE) or 0(FALSE).
Operator |
Meaning |
Example |
== |
Equality operator |
5==3 returns 0 |
> |
Greater Than |
5>3 returns 1 |
< |
Less Than |
5<3 returns 0 |
>= |
Greater Than Equal TO |
5>=3 returns 1 |
<= |
Less Than Equal To |
3<=3 returns 1 |
!= |
Not Equal To |
2!=2 returns 0 |
Write a C program demonstrating the use of relational operators.
#include <stdio.h>
int main() {
// Declare and initialize variables
int a = 5, b = 10;
// Equal to (==)
printf("%d == %d is %d\n", a, b, a == b);
// Not equal to (!=)
printf("%d != %d is %d\n", a, b, a != b);
// Greater than (>)
printf("%d > %d is %d\n", a, b, a > b);
// Less than (<)
printf("%d < %d is %d\n", a, b, a < b);
// Greater than or equal to (>=)
printf("%d >= %d is %d\n", a, b, a >= b);
// Less than or equal to (<=)
printf("%d <= %d is %d\n", a, b, a <= b);
return 0;
}
Logical operators in C are used to perform logical operations on Boolean values (true or false). These operators allow you to combine and manipulate Boolean expressions.
There are three main logical operators in C:
Returns true (1) if both operands are true; otherwise, it returns false (0).
int result = (5 > 3) && (10 < 15); // Result is true (1)
Returns true (1) if at least one operand is true; returns false (0) only if both operands are false.
int result = (5 > 3) || (10 > 15); // Result is true (1)
Returns true (1) if the operand is false; returns false (0) if the operand is true.
int result = !(5 > 3); // Result is false (0)