# Relational Operators in C with Example

In previous lesson we leant the Simple IF statement. There was no example in that lesson. So let us lean some examples for simple if statement with this lesson:

Relation Operators in C

What are the relational operator in C ?

Like the name ‘Relational’, this operator is using for relationship purpose. So we can compare the values of many variables. There are six types of relational operators are 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)

1.   < (Less than)

This operator is using for comparing size of values of two variables. 2<5 indicates 2 is less than 5. a<b indicate a is less than b.

ExampleFind smallest among two number?

 #include #include void main() { int a = 10, b= 20; if(a

Code explanation:

if(a<b)

If the value of a is greater than the value of b, then the printf statement will work. Else does not print anything. Here the value of a is 10 and b is 20, so printf block will work.

2. > (Greater than)

This operator is like the purpose of less than operator. This  is using for comparing size of values of variables. 3>6 indicates 3 is greater than 6. a>b indicate a is greater than b.

Example: Find largest among two given number

In example of less than operator, values of a and b were static. Here we have to find largest number from given two numbers by user. So we have to accept two numbers.

 #include #include void main() { int a, b; printf(“Enter the first number”); scanf(“%d”,&a); printf(“nEnter the second number”); scanf(“%d”,&b); if(a>b) { printf(“%d is greater than %d”,a,b);      } getch(); }

Here the printf statement will work if only the (a>b) condition is true.

Sample Output: 3. == (Equal to)

This operator is using to check two values are equal or not. Note, here twoequal to‘ symbols (= =) are the symbol of this operator. Oneequal to‘ symbol (=) is used as Assignment operator that we leant early.
(a==b) indicate the value of a is equal to the value of b.

Example: Accept two number and check their equality:

 #include #include void main() { int a, b; printf(“Enter the first number”); scanf(“%d”,&a); printf(“nEnter the second number”); scanf(“%d”,&b); if(a==b) { printf(“Both are equal”); } getch(); }

The message ‘Both are equal‘ will display only if both the values of a and b are equal.

Sample Output: 4. <= (Less than or equal to)

This operator is the combination of ‘Less than’ operator and ‘Equal to Operator’. (a<=b) denote the value of a is less than or equal to the value of b.

Example: Implement the Less than or equal to operator with two given numbers

 #include #include void main() { int a, b; printf(“Enter the first number”); scanf(“%d”,&a); printf(“nEnter the second number”); scanf(“%d”,&b); if(a<=b) { printf(“%d is less than or equal to %d”,a,b); } getch(); }

Sample Output: 5. >= (Greater than or equal to)

This operator is the combination of ‘Greater than’ operator and ‘Equal to Operator’. (a>=b) denote the value of a is greater than or equal to the value of b.

Example: Implement the Greater than or equal to operator with two given numbers

 #include  #include void main() { int a, b; printf(“Enter the first number”); scanf(“%d”,&a); printf(“nEnter the second number”); scanf(“%d”,&b); if(a>=b) { printf(“%d is greater than or equal to %d”,a,b); } getch(); }

Sample Output: 6.  != (Not equal to)

This operator is used to determine the values are not equal. (a!=b) means the value of a is not equal to the value of b.

Example: Accept two numbers and check whether both are not equal

 #include #include void main() { int a, b; printf(“Enter the first number”); scanf(“%d”,&a); printf(“nEnter the second number”); scanf(“%d”,&b); if(a!=b) { printf(“%d and %d are not equal”,a,b); } getch(); }

Sample Output: 