Arithmetic operators in C language

 Relational operators are used to comparing two values and return a result of either true or false. There are six relational operators in C

  1. Less than (<)
  2. Greater than (>)
  3. Less than or equal to (<=)
  4. Greater than or equal to (>=)
  5. Equal to (==)
  6. Not equal to (!=)
Operator
Description
<
Less than
<
Greater than
<=
>=
Greater than or equal to
==
Equal to
!=
Not equal to

Less than operator (<)

The less than operator (<) is a relational operator in C used to compare two values. It returns a value of 1 if the first operand is less than the second operand and 0 otherwise.

Example:

#include <stdio.h>

int main() {
   int a = 10;
   int b = 5;

   if (a < b) {
      printf("%d is less than %d", a, b);
   } else {
      printf("%d is not less than %d", a, b);
   }

   return 0;
}

Output:

10 is not less than 5

In this example, we have two integer variables a and b. We use the less than operator in the if statement to compare the values of a and b. Since a is not less than b, the else statement is executed and the output is “10 is not less than 5”.

Greater than operator (>)

The greater than operator (>) is a relational operator used to compare two values. It returns true (1) if the left operand is greater than the right operand, otherwise, it returns false (0).

Example:

#include <stdio.h>

int main() {
    int x = 10, y = 5;

    if (x > y) {
        printf("x is greater than y\n");
    } else {
        printf("y is greater than or equal to x\n");
    }

    return 0;
}

Output:

x is greater than y

In this example, we declare two integer variables x and y and then use the greater than operator to compare them in an if-else statement. Since x is greater than y, the program will print “x is greater than y”.

Less than or equal to (<=)

The less than or equal to operator (<=) is used to compare whether the left operand is less than or equal to the right operand. It returns 1 if the condition is true and 0 if the condition is false.

Example:

#include <stdio.h>

int main() {
   int a = 5, b = 10;

   if(a <= b) {
      printf("a is less than or equal to b\n");
   } else {
      printf("a is greater than b\n");
   }

   return 0;
}

Output:

a is less than or equal to b

In this example, we have two variables a and b, with a having a value of 5 and b having a value of 10. We then use the less than or equal to an operator to compare the values of a and b in the if statement. Since a is less than or equal to b, the condition evaluates to true and the statement “a is less than or equal to b” is printed to the console.

Greater than or equal to (>=)

The greater than or equal to operator (>=) is used to compare two values. It checks whether the left operand is greater than or equal to the right operand. If the left operand is greater than or equal to the right operand, then the expression returns true, otherwise, it returns false.

Example:

#include <stdio.h>

int main() {
   int a = 10, b = 5;
   if (a >= b) {
      printf("a is greater than or equal to b");
   } else {
      printf("a is less than b");
   }
   return 0;
}

Output:

a is greater than or equal to b

Equal to (==)

The equal to operator is denoted by ‘==’ and is used to compare if two values are equal or not. If the two values being compared are equal, then the condition becomes true, and if they are not equal, then the condition becomes false.

Example:

#include <stdio.h>

int main() {
  int num1 = 10;
  int num2 = 20;
  
  if(num1 == num2) {
    printf("num1 is equal to num2");
  } else {
    printf("num1 is not equal to num2");
  }
  
  return 0;
}

Output:

num1 is not equal to num2

Equal to (!=)

The “not equal to” operator in C is represented by the symbol “!=”. It checks if the value of two operands is not equal, and returns a boolean value of either true or false.

Example:

#include <stdio.h>

int main() {
   int a = 10;
   int b = 20;
   
   if (a != b) {
      printf("a is not equal to b\n");
   } else {
      printf("a is equal to b\n");
   }
   
   return 0;
}

Output:

a is not equal to b