Home » C# » C# programs » C# basic programs

C# program to demonstrate example of relational operators

C# example for relational operators: Here, we are writing a C# program to demonstrate example of relational operators.
Submitted by IncludeHelp, on April 06, 2019

Relational operators are used to compare the values and returns Boolean values, if condition is true – they return True, otherwise they return False.

Here is the list of relation operators,

  1. "==" (Equal To) – it returns true, if both operand’s values are equal
  2. "!=" (Not Equal To) – it returns true, if both operand’s values are not equal
  3. "<" (Less Than) – it returns true if first, operand is less than second operand
  4. "<=" (Less Than or Equal To) – it returns true, if either first operand is less than or equal to second operand
  5. ">" (Greater Than) – it returns true, if first operand is greater than second operand
  6. ">=" (Greater Than or Equal To) – it returns true, if either first operand is greater than or equal to second operand

Syntax:

    Operand1 == Operand2
    Operand1 != Operand2
    Operand1 < Operand2
    Operand1 <= Operand2
    Operand1 > Operand2
    Operand1 >= Operand2

Example:

    Input:
    int a = 10;
    int b = 3;
    
    Console.WriteLine("a==b: {0}", (a == b));
    Console.WriteLine("a!=b: {0}", (a != b));
    Console.WriteLine("a>b : {0}", (a > b));
    Console.WriteLine("a>=b: {0}", (a >= b));
    Console.WriteLine("a<b : {0}", (a < b));
    Console.WriteLine("a<=b: {0}", (a <= b));

    Output:
    a==b: False
    a!=b: True
    a>b : True
    a>=b: True
    a<b : False
    a<=b: False

C# code to demonstrate example of relational operators

// C# program to demonstrate example of 
// relational operators
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {

            int a = 10;
            int b = 3;

            //printing return type
            Console.WriteLine("Return type of == operator: {0}", (a == b).GetType());
            Console.WriteLine("Return type of != operator: {0}", (a != b).GetType());
            Console.WriteLine("Return type of > operator : {0}", (a > b).GetType());
            Console.WriteLine("Return type of >= operator: {0}", (a >= b).GetType());
            Console.WriteLine("Return type of < operator : {0}", (a < b).GetType());
            Console.WriteLine("Return type of <= operator: {0}", (a <= b).GetType());

            //printing return values
            Console.WriteLine("a==b: {0}", (a == b));
            Console.WriteLine("a!=b: {0}", (a != b));
            Console.WriteLine("a>b : {0}", (a > b));
            Console.WriteLine("a>=b: {0}", (a >= b));
            Console.WriteLine("a<b : {0}", (a < b));
            Console.WriteLine("a<=b: {0}", (a <= b));

            //checking conditions
            if (a == b)
                Console.WriteLine("a is equal to b");
            else
                Console.WriteLine("a is not equal to b");
            if (a != b)
                Console.WriteLine("a is not equal to b");
            else
                Console.WriteLine("a is equal to b");
            if (a > b)
                Console.WriteLine("a is greater than b");
            else
                Console.WriteLine("a is not greater than b");
            if (a >= b)
                Console.WriteLine("a is greater than or equal to b");
            else
                Console.WriteLine("a is not greater than or equal to b");
            if (a < b)
                Console.WriteLine("a is less than b");
            else
                Console.WriteLine("a is not less than b");
            if (a <= b)
                Console.WriteLine("a is less than or equal to b");
            else
                Console.WriteLine("a is not less than or equal to b");

            //checking conditions in another way
            if ((a == b) == true)
                Console.WriteLine("a is equal to b");
            else
                Console.WriteLine("a is not equal to b");
            if ((a != b) == true)
                Console.WriteLine("a is not equal to b");
            else
                Console.WriteLine("a is equal to b");
            if ((a > b) == true)
                Console.WriteLine("a is greater than b");
            else
                Console.WriteLine("a is not greater than b");
            if ((a >= b) == true)
                Console.WriteLine("a is greater than or equal to b");
            else
                Console.WriteLine("a is not greater than or equal to b");
            if ((a < b) == true)
                Console.WriteLine("a is less than b");
            else
                Console.WriteLine("a is not less than b");
            if ((a <= b) == true)
                Console.WriteLine("a is less than or equal to b");
            else
                Console.WriteLine("a is not less than or equal to b");

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

Return type of == operator: System.Boolean
Return type of != operator: System.Boolean
Return type of > operator : System.Boolean
Return type of >= operator: System.Boolean
Return type of < operator : System.Boolean
Return type of <= operator: System.Boolean
a==b: False
a!=b: True
a>b : True
a>=b: True
a<b : False
a<=b: False
a is not equal to b
a is not equal to b
a is greater than b
a is greater than or equal to b
a is not less than b
a is not less than or equal to b
a is not equal to b
a is not equal to b
a is greater than b
a is greater than or equal to b
a is not less than b
a is not less than or equal to b





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.