C# Comparison Operators

The C# comparison operator is used to compare 2 operands. It returns true or false based on comparison. the whole list of comparison operators square measure listed in an exceedingly table.

Consider x is a variable and therefore the worth assigned the x=2 then,

Operator Name Examples
< Less than x<5 (returns true)
> Greater than x>5 (returns false)
<= Less than equal to x<=2 (returns true)
>= Greater than equal to x>=2 (returns true)
== Equal equal to x==2 (returns true)
!= Not equal to x!=2 (returns false)
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

namespace Comparison_Operator_Example
 class Program
 static void Main(string[] args)
 int n1, n2;

//Accepting two inputs from the user
 Console.Write("Enter first number\t");
 n1 = Convert.ToInt32(Console.ReadLine());
 Console.Write("Enter second number\t");
 n2 = Convert.ToInt32(Console.ReadLine());

//Processing comparison
 //Check whether num1 is greater than or not
 if (n1 > n2)
 Console.WriteLine("{0} is greater than {1}", num1, num2);
 //Check whether n2 is greater than or not
 else if (n2 > n1)
 Console.WriteLine("{0} is greater than {1}", n2, n1);
 Console.WriteLine("{0} and {1} are equal", n1, n2);

Leave a Reply

Your email address will not be published. Required fields are marked *