Computer >> Computer tutorials >  >> Programming >> C programming

What are relational operators in C language?


These are used for comparing the two expressions.

OperatorDescriptionExamplea=10,b=20Output
<less thana<b10<201
<=less than (or) equal toa<=b10<=201
>greater thana>b10>200
>=greater than (or) equal toa>=b10>=200
==equal toa==b10==200
!=not equal toa!=b10!=201

Relational expression output is either true (1) (or) false (0).

Algorithm

Follow the algorithm given below −

START
Step 1: Declare integer variables.
Step 2: Read all variables at runtime.
Step 3: Perform relational operations.
   i. a<b
   ii. a<=b
   iii. a>b
   iv. a>=b
   v. a==b
   vi. a!=b
Step 4: Print all computed values.

Example

Following is the C program to compute relational operators −

#include<stdio.h>
main (){
   int a,b;
   printf("enter a,b values:\n");
   scanf("%d%d",&a,&b);
   printf("%d\n",a<b);
   printf("%d\n",a<=b);
   printf("%d\n",a>b);
   printf("%d\n",b>=a);
   printf("%d\n",a==b);
   printf("%d\n",a!=b);
}

Output

You will see the following output −

enter a,b values:
120 340
1
1
0
1
0
1