# 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.

**< (Less than)****> (Greater than)****<= (Less than or equal to)****>= (Greater than or equal to)****== (Equal to)****!= (Not equal to)**

__1. < (Less than)__

This operator is using for comparing size of values of two variables.

If the value of

This operator is like the purpose of less than operator. This is using for comparing size of values of variables.

In example of less than operator, values of

Here the printf statement will work if only the

Sample Output:

This operator is using to check two values are equal or not. Note, here

The message '

Sample Output:

This operator is the

Sample Output:

This operator is the

Sample Output:

This operator is used to determine the values are not equal. (

Sample Output:

**2<5**indicates 2 is less than 5.**a<b**indicate**a**is less than**b**.**Example**:*Find smallest among two number?*#include<stdio.h> #include<conio.h> void main() { int a = 10, b= 20; if(a<b) { printf(“10 is less than 20”); } getch(); } |

**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<stdio.h> #include<conio.h> 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

**two**'**equal to**' symbols (**= =**) are the symbol of this operator.**One**'**equal 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<stdio.h> #include<conio.h> 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<stdio.h> #include<conio.h> 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 tha**n’ 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<stdio.h> #include<conio.h> 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<stdio.h> #include<conio.h> 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:

## No comments:

## Post a Comment