How to find minimum between 2 numbers using C#?

CsharpProgrammingServer Side Programming

Firstly, declare and initialize two numbers.

int num1 = 35;
int num2 = 55;

With that, use if- else to find the minimum number.

if (num1 < num2) {
   minNum = num1;
} else {
   minNum = num2;
}

Above, we have set the minimum value to the variable minNum and printed it later on.

The following is the complete example to find minimum between 2 numbers in C#.

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         int num1 = 50;
         int num2 = 90;
         int minNum;
         Console.WriteLine("Number 1: "+num1);
         Console.WriteLine("Number 2: "+num2);
         if (num1 < num2) {
            minNum = num1;
         } else {
            minNum = num2;
         }
         Console.WriteLine("Minimum number is: "+minNum);
         Console.ReadKey() ;
      }
   }
}

Output

Number 1: 50
Number 2: 90
Minimum number is: 50
raja
Published on 18-Sep-2018 08:12:29
Advertisements