How to find minimum between 2 numbers using C#?


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements