Thursday, 3 July 2014

Use of Relational Operators in C Programs

In programming it may happen sometime that control have to take decision. But problem is in programming there is two conditions. “True” and “False”. So we need help to express that condition. Relational Operators help us in this stage. There are total 6 type of rational operators
X==Y if true X is equal Y
X>Y if true X is greater then Y
Y<X if true Y is less than X
X!=Y if true X is not equal to Y
X=>Y if true X is greater than equal to Y
X=<Y if true Y is greater than equal to X
Let’s see an example with “if” statement
#include<stdio.h>
void main()
{
    int a,b;
    printf("Enter value for a\n");
    scanf("%d",&a);
    printf("Enter value for b\n");
    scanf("%d",&b);
    if(a==b)
    {
        printf("a is equal to b\n");
    }
    if(a>b)
    {
        printf("a is greater than b\n");
    }
    if(a<b)
    {
        printf("a is less than b\n");
    }
    if(a>=b)
    {
        printf("a is greater than equal to b\n");
    }
    if(a<=b)
    {
        printf("a is less than equal to b\n");
    }
    if(a!=b)
    {
        printf("a is not equal to b\n");
    }
}
The program is written in this way that if condition is true then that will be printed but if not true then not printed. So here the expression goes through all “if” statements. Now let’s see the output
Enter value for a
34
Enter value for b
23
a is grater then b
a is grater then equal to b
a is not equal to b
So all true condition is printed.

0 comments:

Post a Comment