Home »
.Net »
C# Programs
C# - Find Maximum and Minimum of Two Numbers
Read two numbers, we have to find the maximum and minimum number between them.
Submitted by IncludeHelp, on April 09, 2019 [Last updated : March 17, 2023]
C# program to find the maximum and minimum number between two numbers
Here, we are asking for two integer numbers, finding the maximum and minimum and maximum number using if-else statement and the ternary operator.
// C# program to read two numbers
// and find maximum, minimum number
using System;
using System.IO;
using System.Text;
namespace IncludeHelp {
class Test {
// Main Method
static void Main(string[] args) {
int a;
int b;
int min;
int max;
//input numbers
Console.Write("Enter first number : ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
b = Convert.ToInt32(Console.ReadLine());
//finding max number using if-else
if (a > b)
max = a;
else
max = b;
//finding min number using if else
if (a < b)
min = a;
else
min = b;
//printing
Console.WriteLine("Using if-else...");
Console.WriteLine("Minimum number = {0}", min);
Console.WriteLine("Maximum number = {0}", max);
//finding max number using ternary operator
max = (a > b) ? a : b;
//finding min number using ternary operator
min = (a < b) ? a : b;
//printing
Console.WriteLine("Using Ternary operator...");
Console.WriteLine("Minimum number = {0}", min);
Console.WriteLine("Maximum number = {0}", max);
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output
Enter first number : 100
Enter second number: 200
Using if-else...
Minimum number = 100
Maximum number = 200
Using Ternary operator...
Minimum number = 100
Maximum number = 200
More examples...
C# Basic Programs »